C# 根据XDocument的子元素对其进行排序

C# 根据XDocument的子元素对其进行排序,c#,linq-to-xml,C#,Linq To Xml,我将以下XML(以缩写形式显示)加载到XDocument对象中 <employees> <employee> <identification> <firstName>John</firstName> <lastName>Smith</lastName> </identification> <payment contractType="1">

我将以下XML(以缩写形式显示)加载到XDocument对象中

<employees>
  <employee>
    <identification>
      <firstName>John</firstName>
      <lastName>Smith</lastName>
    </identification>
    <payment contractType="1">
      <income type="4" startDate="2014-10-01" endDate="2014-10-31">
      </income>
    </payment>
  </employee>
  <employee>
    <identification>
      <firstName>John</firstName>
      <lastName>Balmer</lastName>
    </identification>
    <payment contractType="2">
      <income type="2" startDate="2014-10-01" endDate="2014-10-31">
      </income>              
    </payment>
  </employee>
</employees>

约翰
史密斯
约翰
巴尔默
我想在XDocument对象上应用linq查询,该查询根据
然后使用linq to XML对所有节点进行排序。因此,在应用linq之后,我的XDocument对象将包含以下XML:

<employees>
  <employee>
    <identification>
      <firstName>John</firstName>
      <lastName>Balmer</lastName>
    </identification>
    <payment contractType="2">
      <income type="2" startDate="2014-10-01" endDate="2014-10-31">
      </income>
    </payment>
  </employee>
  <employee>
    <identification>
      <firstName>John</firstName>
      <lastName>Smith</lastName>
    </identification>
    <payment contractType="1">
      <income type="4" startDate="2014-10-01" endDate="2014-10-31">
      </income>              
    </payment>
  </employee>
</employees>

约翰
巴尔默
约翰
史密斯

快速和肮脏,只是给你一个想法:

var sorted = new XDocument(
                new XElement(
                   "employees",
                   ORIGINAL_XDOC.Descendants("employee")
                                .OrderBy(e => e.Descendants("lastName")
                                               .Single()
                                               .Value)
                                .ThenBy(e => e.Descendants("firstName")
                                              .Single()
                                              .Value)));

到目前为止有任何尝试吗?@decPL我尝试了orderby e.Element(XMLGenerator.nsDefault+“identification”).Element(XMLGenerator.nsDefault+“lastname”).Value之类的内容,但我得到了一个内部值exception@decPL其中->e=来自xDoc.subjects中的e(XMLGenerator.nsDefault+“employee”)我会简化你的模式,这是不可能的。模式是由客户按原样提供的。我认为您向后获得了
firstName
lastName
。@decPL您的代码在我的data@GiorgosBetsos这意味着您的XML可能会有所不同——我已经在您在LINQPAD中提供的XML上测试了这一点,并且效果良好。请验证-如果你仍然有问题,我明天会在场外发布完整的代码示例。我不敢相信。这正是我在代码中一直在做的事情(请参阅相关评论)。一旦我将你的答案粘贴到我的项目中,它就可以毫无例外地工作了。你只需将firstName与lastNameha切换就可以了。。。我把lastName写在我的代码中作为lastName。。。argghhh@GiorgosBetsos,很遗憾听到这个消息:)
// Extract "employee" Xelements in order.
var orderedEmployees = 
    from employee in xml.Descendants("employee")
    orderby employee.Element("identification").Element("lastName").Value,
            employee.Element("identification").Element("firstName").Value
    select employee;

// Build a new Xelement with the original root and orderded "employee" elements.
var result = new XElement(xml.Root.Name,
                 from employee in orderedEmployees
                 select new XElement(employee)
             );