<Root xmlns="http://tempuri.org/DataSourceSchemaConfig.xsd">
<Node>
<Name>Peter</Name>
</Node>
<Node>
<Name>John</Name>
</Node>
</Root>
如何获取姓名列表?
我一直在尝试这个,但它不起作用,我的错误在哪里?
var lists = from node in nodes.Descendants()
where node.Name.LocalName.Equals("Node")
select node.Elements("Name").First().Value;
L.B 解决方案仅在我从 ROOT 标记中删除 xmlns="http://tempuri.org/DataSourceSchemaConfig.xsd"时才有效。
最佳答案
XDocument xDoc = XDocument.Load(....);
var names = xDoc.Descendants("Name").Select(x => x.Value);
--编辑--
XDocument xDoc = XDocument.Load(....);
XNamespace ns = XNamespace.Get("http://tempuri.org/DataSourceSchemaConfig.xsd");
var names = xDoc.Descendants(ns+"Name").Select(x => x.Value);
关于c# - LINQ TO XML 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727289/