我有以下 xml 代码:
<highscore>
<score>
<naam>Pipo</naam>
<punten>200</punten>
</score>
<score>
<naam>Harry</naam>
<punten>400</punten>
</score>
</highscore>
如何按“punten”排序?所以最高的数字在上面?
谢谢!!
var xml=@" <highscore>
<score>
<naam>Pipo</naam>
<punten>200</punten>
</score>
<score>
<naam>Harry</naam>
<punten>400</punten>
</score>
</highscore>";
var doc = XDocument.Parse(xml);
var orderedScoreElements =
doc.Root
.Elements("score")
.OrderByDescending(e => (int)e.Element("punten"))
.ToList();
并按顺序重写文档:
doc.Root.RemoveNodes();
doc.Root.Add(orderedScoreElements);
var xml = @"<highscore>
<score>
<naam>Pipo</naam>
<punten>200</punten>
</score>
<score>
<naam>Harry</naam>
<punten>400</punten>
</score>
</highscore>";
var x = XDocument.Parse(xml);
var elementsInOrder = x.Descendants("score").OrderByDescending(el=>Convert.ToInt32(el.Element("punten").Value));