c# - LINQ TO XML 解决方案

标签 c# linq-to-xml

<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/

相关文章:

c# - string.GetHashCode() 唯一性和冲突

c# - 我应该如何存储我的 ASP.NET MVC 网站的设置?

c# - WCF,svcutil.exe : How to properly match or configure web service client code?

c# - Visual Studio 尝试在编辑器中运行该程序并崩溃

c# - Linq - 无法将类型 'IEnumerable<IEnumerable<.XElement>>' 隐式转换为 'IEnumerable<.XElement>'

c# - 如何使用 LINQ 选择内部 XML 节点的集合?

c# - 更改字符串对象长度(通过突变!)的不安全代码?

c# - Linq-to-xml 按没有子元素的元素分组

c# - 如何在针对模式验证的 XDocument 中查找无效的 XML 节点(XmlSchemaValidationException.SourceObject 为空)

c# - 选择子元素具有值的 XElement