所以我试图解析一个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" ,否则不会将任何内容写入控制台,之后,我会得到一个属性名称和值的列表,正如我所期望的那样,并且我需要!
编辑:格式化。