我正在爲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