我正在为windows phone的应用程序解析xml。 我的XML看起来像这样:获取XElements的名称
<?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);
但是我需要像总体,童年等人节点的名称。 – 2012-07-26 13:35:27
而foreach循环应该是这样的:foreach(var item in data) { XElement el =(item as XElement); foreach(el.Elements()中的变量) {+}(= XElement).Name.LocalName; } } – 2012-07-26 13:50:26