-2

我有以下 xml 代码:

    <highscore>
  <score>
    <naam>Pipo</naam>
    <punten>200</punten>
  </score>
  <score>
    <naam>Harry</naam>
    <punten>400</punten>
  </score>
</highscore>

如何按“punten”排序?所以最高的数字在上面?

谢谢!!

4

2 回答 2

4
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);
于 2013-06-21T16:48:24.453 回答
2
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));
于 2013-06-21T16:48:46.730 回答