我正在 Windows Phone 的应用程序中解析 xml。 我的 XML 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person name="Kurt Cobain">
<overall>
</overall>
<childhood>
</childhood>
<youth>
</youth>
<picture1>
</picture1>
</person>
</people>
我应该获取人员节点元素的名称(总体、童年、青年等),因为对于每个人员节点,它们都是不同的。 这是到目前为止我的代码,但查询结果为空:
XDocument loadedXml = XDocument.Load("people.xml");
var data = from query in loadedXml.Descendants("person")
where ((query.Attribute("name").Value as string).Equals("Kurt Cobain"))
select query.Elements();
string test = "";
foreach (var item in data)
{
test + = (item as XElement).Name.LocalName;
}
MessageBox.Show(test);
最佳答案
从 linq 中删除 .Elements()
。
var data = from query in loadedXml.Descendants("person")
where ((query.Attribute("name").Value as string).Equals("Kurt Cobain"))
select query;
关于c# - 获取 XElement 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670115/