LINQ to XML 查询没有 return 任何结果
LINQ to XML query doesn't return any results
我正在尝试查询 XML 树并将生成的元素值存储在类似于 this example 的对象列表中。
问题是无论我尝试什么,我都无法填充列表。我没有任何错误,我只有一个空列表。我几乎可以肯定我的查询设置有误,但我是 LINQ 的新手,我不知道哪里出了问题。这是我正在使用的 class:
public class Individual
{
public string field1 { get; set; }
public string field2 { get; set; }
public bool field3 { get; set; }
}
这是我的变量和查询:
XDocument xmlDoc = XDocument.Load(new System.IO.StringReader(MainDataSource.CreateNavigator().OuterXml));
xmlDoc.Descendants()
.Attributes()
.Where(x => x.IsNamespaceDeclaration)
.Remove();
List<Individual> individualList =
(
from el in xmlDoc.Root.Elements("myFields").Descendants("Individual")
select new Individual
{
field1 = (string)el.Element("field1"),
field2 = (string)el.Element("field2"),
field3 = (bool)el.Element("field3")
}
).ToList();
这是 XML 文档:
<?mso-infoPathSolution solutionVersion="1.0.0.31" productVersion="15.0.0" PIVersion="1.0.0.0" href="file:///C:\Users\User\AppData\Local\Microsoft\InfoPath\Designer41d50f99c274f7c\manifest.xsf" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<myFields xml:lang="en-us" xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2021-02-26T15:29:19">
<DirectReportsList>
<Individual>
<field1>foo</field1>
<field2>bar</field2>
<field3>true</field3>
</Individual>
</DirectReportsList>
<Current_UserID></Current_UserID>
<Current_UserName></Current_UserName>
</myFields>
您的代码正在寻找根下的 myFields
元素,但那是根,因此未找到任何元素。
你可以做什么:
XNamespace ns ="http://schemas.microsoft.com/office/infopath/2003/myXSD/2021-02-26T15:29:19";
List<Individual> individualList =
(
from el in xmlDoc.Root.Descendants(ns + "Individual")
select new Individual
{
field1 = (string)el.Element(ns + "field1"),
field2 = (string)el.Element(ns +"field2"),
field3 = (bool)el.Element(ns +"field3")
}
).ToList();
}
如果您真的不想处理命名空间,您可以使用元素的 LocalName,但它很容易受到意外 xml 内容的影响。
Update
我刚刚查看了您链接到的示例。不行,作者发错代码了。
我正在尝试查询 XML 树并将生成的元素值存储在类似于 this example 的对象列表中。
问题是无论我尝试什么,我都无法填充列表。我没有任何错误,我只有一个空列表。我几乎可以肯定我的查询设置有误,但我是 LINQ 的新手,我不知道哪里出了问题。这是我正在使用的 class:
public class Individual
{
public string field1 { get; set; }
public string field2 { get; set; }
public bool field3 { get; set; }
}
这是我的变量和查询:
XDocument xmlDoc = XDocument.Load(new System.IO.StringReader(MainDataSource.CreateNavigator().OuterXml));
xmlDoc.Descendants()
.Attributes()
.Where(x => x.IsNamespaceDeclaration)
.Remove();
List<Individual> individualList =
(
from el in xmlDoc.Root.Elements("myFields").Descendants("Individual")
select new Individual
{
field1 = (string)el.Element("field1"),
field2 = (string)el.Element("field2"),
field3 = (bool)el.Element("field3")
}
).ToList();
这是 XML 文档:
<?mso-infoPathSolution solutionVersion="1.0.0.31" productVersion="15.0.0" PIVersion="1.0.0.0" href="file:///C:\Users\User\AppData\Local\Microsoft\InfoPath\Designer41d50f99c274f7c\manifest.xsf" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<myFields xml:lang="en-us" xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2021-02-26T15:29:19">
<DirectReportsList>
<Individual>
<field1>foo</field1>
<field2>bar</field2>
<field3>true</field3>
</Individual>
</DirectReportsList>
<Current_UserID></Current_UserID>
<Current_UserName></Current_UserName>
</myFields>
您的代码正在寻找根下的 myFields
元素,但那是根,因此未找到任何元素。
你可以做什么:
XNamespace ns ="http://schemas.microsoft.com/office/infopath/2003/myXSD/2021-02-26T15:29:19";
List<Individual> individualList =
(
from el in xmlDoc.Root.Descendants(ns + "Individual")
select new Individual
{
field1 = (string)el.Element(ns + "field1"),
field2 = (string)el.Element(ns +"field2"),
field3 = (bool)el.Element(ns +"field3")
}
).ToList();
}
如果您真的不想处理命名空间,您可以使用元素的 LocalName,但它很容易受到意外 xml 内容的影响。
Update
我刚刚查看了您链接到的示例。不行,作者发错代码了。