c# – 使用LINQ排序

我正在努力进行这种排序,需要一些帮助.

我将按排名对子节点进行排序并保存.
例如为了命令主元素内的子节点,我将传递特定部分的ID.

此示例仅适用于第一个后代,现在我卡住了.

XElement x = XElement.Load(xmlString1);
x.Descendants("opt").First().ReplaceNodes(x.Descendants("opt").First()
 .Descendants("sel").OrderBy(o => int.Parse(o.Attribute("rank").Value)));
4x.Save(xmlString2);

我需要那样的.

x.Descendants("sub").Where(b => b.Attribute("id").Value == "DFG")
 .ReplaceNodes(x.Descendants("opt").First()
 .Descendants("sel").OrderBy(o => int.Parse(o.Attribute("rank").Value))

原版的

  <main id="AFB" rank="1" name="ROOT">
<sub id="DFG" rank="2" name="SUB1">
 <att >
    <sel id="JIK" rank="4" name="444" />
    <sel id="OKI" rank="2" name="222" />
  </att>
  <opt>
    <sel id="JIK" rank="2" name="122" />
    <sel id="OKI" rank="1" name="111" />
  </opt>
</sub>  
 <sub id="EGG" rank="1" name="SUB2" >
  <opt>
    <sel id="DJI" rank="1" name="111" />
    <sel id="LOW" rank="3" name="333" />
    <sel id="QWE" rank="2" name="222" />
  </opt>
</sub>
<main>

目标

 <main id="AFB" rank="1" name="ROOT">   
 <sub id="EGG" rank="1" name="SUB2" >
  <opt>
    <sel id="DJI" rank="1" name="111" />        
    <sel id="QWE" rank="2" name="222" />
    <sel id="LOW" rank="3" name="333" />
  </opt>
</sub>
<sub id="DFG" rank="2" name="SUB1">
  <att >
    <sel id="OKI" rank="2" name="222" />
    <sel id="JIK" rank="4" name="444" />        
  </att>
  <opt>
    <sel id="OKI" rank="1" name="111" />
    <sel id="JIK" rank="2" name="122" />        
  </opt>
</sub>
<main>

最佳答案 我觉得你有错字.但是,看看这个解决方案:

var text = @"
<main id='AFB' rank='1' name='ROOT'>
    <sub id='DFG' rank='2' name='SUB1'>
        <opt>
            <sel id='JIK' rank='4' name='444' />
            <sel id='OKI' rank='2' name='222' />
        </opt>
        <opt>
            <sel id='JIK' rank='2' name='122' />
            <sel id='OKI' rank='1' name='111' />
        </opt>
    </sub>  
    <sub id='EGG' rank='1' name='SUB2' >
        <opt>
            <sel id='DJI' rank='1' name='111' />
            <sel id='LOW' rank='3' name='333' />
            <sel id='QWE' rank='2' name='222' />
        </opt>
    </sub>
</main>";

var x = XDocument.Parse(text);
x.Root.ReplaceNodes(x.Descendants("sub").OrderBy(a => int.Parse(a.Attribute("rank").Value)));
foreach (var opt in x.Descendants("opt"))
    opt.ReplaceNodes(opt.Descendants("sel").OrderBy(a => int.Parse(a.Attribute("rank").Value)));

此时x包含以下XML:

<main id="AFB" rank="1" name="ROOT">
  <sub id="EGG" rank="1" name="SUB2">
    <opt>
      <sel id="DJI" rank="1" name="111" />
      <sel id="QWE" rank="2" name="222" />
      <sel id="LOW" rank="3" name="333" />
    </opt>
  </sub>
  <sub id="DFG" rank="2" name="SUB1">
    <opt>
      <sel id="OKI" rank="2" name="222" />
      <sel id="JIK" rank="4" name="444" />
    </opt>
    <opt>
      <sel id="OKI" rank="1" name="111" />
      <sel id="JIK" rank="2" name="122" />
    </opt>
  </sub>
</main>

如果att而不是’opt`并且应该包括在内,则以下方法将起作用:

var x = XDocument.Parse(text);
x.Root.ReplaceNodes(x.Descendants("sub").OrderBy(a => int.Parse(a.Attribute("rank").Value)));
foreach (var opt in x.Descendants("sub").Elements())
    opt.ReplaceNodes(opt.Descendants("sel").OrderBy(a => int.Parse(a.Attribute("rank").Value)));

如果需要按名称对单个元素进行排序,请使用以下内容(如果不存在任何排名属性或为空,则将其放在后面):

//sub with id=EGG
var sub2 = x.Descendants("sub").FirstOrDefault(a => a.Attribute("id").Value == "EGG");
if (sub2 != null)
{
    foreach (var node in sub2.Elements())
        node.ReplaceNodes(node.Elements().OrderBy(a =>
        {
            int rank;
            if (a.Attribute("rank") == null || !int.TryParse(a.Attribute("rank").Value, out rank))
                rank = int.MaxValue;
            return rank;
        }));
}
点赞