【发布时间】:2010-03-11 14:27:18
【问题描述】:
所以我正在尝试解析一个 xml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<att1 name="bob" age="unspecified" xmlns="http://foo.co.uk/nan">
</att1>
</Root>
使用以下代码:
XElement xDoc= XElement.Load(filename);
var query = from c in xDoc.Descendants("att1").Attributes() select c;
foreach (XAttribute a in query)
{
Console.WriteLine("{0}, {1}",a.Name,a.Value);
}
除非我从 xml 文件中删除 xmlns="http://foo.co.uk/nan" ,否则不会向控制台写入任何内容,之后,我会得到预期的属性名称和值列表,并且根据我的需要!
编辑:格式化。
【问题讨论】:
-
顺便说一句,写
from c in whatever select c是没有意义的。 -
所以没有!谢谢 :) 我是从一个非常有用的 LINQ to XML 介绍中挑选出来的:microsoft.com/uk/msdn/nuggets/nugget/204/…
标签: c# parsing linq-to-xml xml-namespaces xelement