我有以下XML並希望返回所有「學校」子項,但我只獲得第一個子項。 (jeffersion/08.36)我高高低低地ban着頭。我錯過了什麼?使用linq c在單個節點上返回多個具有相同名稱的xml子項#
<users>
<user>
<role>janitor</role>
<schools>
<school_name>jefferson</school_name>
<keycode>80.36</keycode>
<school_name>mainline</school_name>
<keycode>64.36</keycode>
<school_name>south side</school_name>
<keycode>31</keycode>
</schools>
</user>
</users>
這隻返回的第一條記錄。
var results= from schools in myXmlDoc.Descendants("schools")
select new
{
SchoolName = schools.Element("school_name").Value,
KeyCode = schools.Element("keycode").Value
};
我也試過:
var results= (from schools in myXmlDoc.Descendants("schools")
select new
{
SchoolName = schools.Element("school_name").Value,
KeyCode = schools.Element("keycode").Value
}.ToList();
這得到的值,但只有第一個學校:
var schools = (from c in xml.Descendants("user")
select new
{
Name = c.Element("role").Value,
Fields = c.Elements("schools")
.Select(f => new
{
SchoolName = f.Element("school_name").Value,
Keycode = f.Element("keycode").Value
}).ToArray()
}).ToList();
你知道你的XML文件中缺少`>`? mainline school_name> 64.36 `我可以想象爲什麼你不會從一個沒有格式化的xml文件中獲得更多結果。 –
Bazzz
2011-02-03 14:03:12