C# 如何使用XPath查询选择XML节点
我想用C从XMLNodeList对象中的XmlDocument对象中选择所有用户节点m:properties!如何做到这一点 这是我的C代码,其中获取xml文档: 这是我的xml文档格式:C# 如何使用XPath查询选择XML节点,c#,xml,xpath,C#,Xml,Xpath,我想用C从XMLNodeList对象中的XmlDocument对象中选择所有用户节点m:properties!如何做到这一点 这是我的C代码,其中获取xml文档: 这是我的xml文档格式: 我要所有节点→ 条目-对于所有用户信息,您有一个名称空间问题。我建议将XMLLINQ与下面类似的代码字典一起使用 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System
我要所有节点→ 条目-对于所有用户信息,您有一个名称空间问题。我建议将XMLLINQ与下面类似的代码字典一起使用
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());
}
}
}
请编辑您的问题并将XML字符串粘贴为文本好吗?您必须传递完整的xpath,而不仅仅是m:properties。我用m:properties的路径编辑我的问题。
<?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>-
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());
}
}
}