我试图得到所有的Hoop属性,但它只获得第一个值(在这个例子中,它是向列表框中添加24和4).有没有办法将我的所有结果添加到列表中.我通常使用.ToList()但它在这一瞬间不起作用.目的是让Home和客户的篮球分开,然后将它存放到一个物体中.
XML:
<League>
<Round>
<Match>
<Team Side="Home" >
<Hoop qtr="1st" player-name="Joe" time-scored="24" />
<Hoop qtr="1st" player-name="Jack" time-scored="54" />
</Team>
<Team Side="Away">
<Hoop qtr="1st" player-name="James" time-scored="4" />
<Hoop qtr="1st" player-name="Brown" time-scored="34" />
</Team>
</Match>
</Round>
</League>
Run Code Online (Sandbox Code Playgroud)
C#:
XDocument xDoc = XDocument.Load("test.xml");
var query = from q in xDoc.Descendants("Team")
where (string)q.Attribute("Side") == "Home"
let d = q.Element("Hoop")
select new
{
Period = d.Attribute("qtr").Value,
Name = d.Attribute("player-name").Value,
Time = d.Attribute("time-scored").Value
};
foreach (var qq in query)
{
listBox.Items.Add(qq.Time);
}
Run Code Online (Sandbox Code Playgroud)
更改let d = q.Element("Hoop")到from d in q.Elements("Hoop")
var xDoc = XDocument.Load("test.xml");
var query = from q in xDoc.Descendants("Team")
where (string)q.Attribute("Side") == "Home"
from d in q.Elements("Hoop")
select new
{
Period = d.Attribute("qtr").Value,
Name = d.Attribute("player-name").Value,
Time = d.Attribute("time-scored").Value
};
foreach (var qq in query)
{
listBox.Items.Add(qq.Time);
}
Run Code Online (Sandbox Code Playgroud)