【问题标题】:How to select XML node with XPath query如何使用 XPath 查询选择 XML 节点
【发布时间】:2018-07-16 07:00:48
【问题描述】:

我想用 C# 从 XMLNodeList 对象中的 XmlDocument 对象中选择所有用户节点 m:properties!如何做到这一点?

这是我获取 xml 文档的 C# 代码:

XmlDocument navEmployesXML = GetXmlDataFromUrl("MYURL");
XmlNodeList nodelist = navEmployesXML.SelectNodes("/feed/entry"); I try with full path but doesnt working

这是我的 xml 文档格式:

<?xml version="1.0" encoding="UTF-8"?>
-
<feed
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns="http://www.w3.org/2005/Atom" xml:base="http://10.20.1.8:7018/activity/OData/">
    <id>http://12333:7018/activity/OData/EmList</id>
    <title type="text">EmList</title>
    <updated>2018-07-13T14:40:29Z</updated>
    <link title="EmList" href="EmList" rel="self"/>-
    <entry m:etag="W/"'36%3BUBQAAAJ7%2FxAEEgQVBBcEFQQSBBAEAAAAAA%3D%3D7%3B33898870%3B'"">
        <id>http://12333:7018/activity/OData/EmList('%D0%90%D0%92%D0%95%D0%97%D0%95%D0%92%D0%90')</id>
        <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="NAV.EmList"/>
        <link title="EmList" href="EmList('%D0%90%D0%92%D0%95%D0%97%D0%95%D0%92%D0%90')" rel="edit"/>
        <title/>
        <updated>2018-07-13T14:40:29Z</updated>-
        <author>
            <name/>
        </author>-
        <content type="application/xml">-
            <m:properties>
                <d:No>avezeva</d:No>
                <d:Company>My Company</d:Company>
                <d:FullName>Angelina Dangeloa</d:FullName>
                <d:USERID_1>myDomain\AVEZEVA</d:USERID_1>
                <d:Job_Title>Organisator</d:Job_Title>
                <d:Department>MY SOFT</d:Department>
                <d:ManagerID>myDomain\AIVANOV</d:ManagerID>
                <d:Days_of_current_year m:type="Edm.Int32">20</d:Days_of_current_year>
                <d:Days_of_last_year m:type="Edm.Int32">0</d:Days_of_last_year>
                <d:Deputy>myDomain\AVEZEVA;myDomain\MMANASIEVA</d:Deputy>
                <d:Deputy1/>
                <d:Second_possition/>
                <d:Union_Membership_No/>
                <d:ETag>36;UBQAAAJ7/xAEEgQVBBcEFQQSBBAEAAAAAA==7;33898870;</d:ETag>
            </m:properties>
        </content>
    </entry>-

我想要所有节点 → entry - 包含所有用户信息

【问题讨论】:

  • 能否编辑您的问题并将 XML 字符串粘贴为文本?
  • 你必须传递完整的 xpath,而不仅仅是 m:properties
  • 我用 m:properties 的路径编辑我的问题。

标签: c# xml xpath


【解决方案1】:

您遇到了命名空间问题。我建议将 Xml Linq 与下面的代码类似的字典一起使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication53
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string feed = File.ReadAllText(FILENAME);            
            XDocument doc = XDocument.Parse(feed);

            XElement properties = doc.Descendants().Where(x => x.Name.LocalName == "properties").FirstOrDefault();

            Dictionary<string, string> dict = properties.Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多