Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到xml查询不返回任何值_C#_Xml_Linq To Xml - Fatal编程技术网

C# Linq到xml查询不返回任何值

C# Linq到xml查询不返回任何值,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我正在提取SharePoint网站集用户的XML列表,然后尝试查询InnerXml。InnerXml如下所示: <Users xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/"> <User ID="91" Name="Jane Smith" LoginName="domain1\jsmith" /> <User ID="814" Name="Brad Jones" LoginName="

我正在提取SharePoint网站集用户的XML列表,然后尝试查询InnerXml。InnerXml如下所示:

<Users xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/"> 
<User ID="91" Name="Jane Smith" LoginName="domain1\jsmith" />
<User ID="814" Name="Brad Jones" LoginName="domain1\bjones" />
<User ID="1252" Name="Charles Johnson" LoginName="domain2\cjohnson" />
</Users>

root
包含InnerXml文本,因此我认为问题与SharePoint无关。

您没有在
元素调用中指定名称空间。幸运的是,这在LINQ to XML中很容易做到:

XNamespace ns = "http://schemas.microsoft.com/sharepoint/soap/directory/";
...
from el in root.Elements(ns + "User");
如果您知道
ID
属性将始终是一个整数,那么我实际上要澄清这一点,并且在没有帮助时避免使用查询表达式:

// TODO: Find a nicer way of doing this; you shouldn't need to parse it again
XElement root = XElement.Parse(siteUsers.InnerXml);
var siteUserElements = root.Elements(ns + "User")
                           .Where(el => (int) el.Attribute("ID") == 814);

foreach (XElement el in siteUserElements)
{
    Console.WriteLine("el: " + el);
}

您没有在
元素
调用中指定名称空间。幸运的是,这在LINQ to XML中很容易做到:

XNamespace ns = "http://schemas.microsoft.com/sharepoint/soap/directory/";
...
from el in root.Elements(ns + "User");
如果您知道
ID
属性将始终是一个整数,那么我实际上要澄清这一点,并且在没有帮助时避免使用查询表达式:

// TODO: Find a nicer way of doing this; you shouldn't need to parse it again
XElement root = XElement.Parse(siteUsers.InnerXml);
var siteUserElements = root.Elements(ns + "User")
                           .Where(el => (int) el.Attribute("ID") == 814);

foreach (XElement el in siteUserElements)
{
    Console.WriteLine("el: " + el);
}