【问题标题】:LINQ-TO-XML Order Xml by attribute not in top levelLINQ-TO-XML 按不在顶层的属性对 Xml 进行排序
【发布时间】:2013-10-08 23:03:47
【问题描述】:

我有一个这样的 xml 文件:

<Root>
    <Combination Id="A35">
       <Level Id="1">
         <Option Id="S10" Time="07:30" Date="02-10-2013"/>
         <Option Id="S13" Time="08:30" Date="03-10-2013"/>
         <Option Id="S15" Time="08:30" Date="01-10-2013"/>
       </Level1>
       <Level Id="2">
         <Option Id="S25" Time="07:30" Date="02-10-2013"/>
         <Option Id="S26" Time="08:30" Date="03-10-2013"/>
      </Level1>
    </Combination>
    <Combination Id="A23">
        <Level Id="1">
          <Option Id="S13" Time="09:30" Date="02-10-2013"/>
          <Option Id="S8"  Time="07:30" Date="01-10-2013"/>
        </Level>
        <Level Id="2">
          <Option Id="S10" Time="07:30" Date="02-10-2013"/>
          <Option Id="S13" Time="08:30" Date="03-10-2013"/>
        </Level>
    </Combination>
    .....
</Root>

我想按节点选项中的日期和时间属性对其进行排序(每个选项自行排序),但也按 id=1 的级别节点内的某些选项节点中的最早值排序(因此组合节点变化):

<Root>
    <Combination Id="A23">
        <Level Id="1">
          <Option Id="S8"  Time="07:30" Date="01-10-2013"/>
          <Option Id="S13" Time="09:30" Date="02-10-2013"/>
        </Level1>
        <Level Id="2">
          <Option Id="S10" Time="07:30" Date="02-10-2013"/>
          <Option Id="S13" Time="08:30" Date="03-10-2013"/>
        </Level1>
    </Combination>
    <Combination Id="A35">
       <Level Id="1">
         <Option Id="S15" Time="08:30" Date="01-10-2013"/>
         <Option Id="S10" Time="07:30" Date="02-10-2013"/>
         <Option Id="S13" Time="08:30" Date="03-10-2013"/>
       </Level1>
       <Level Id="2">
         <Option Id="S25" Time="07:30" Date="02-10-2013"/>
         <Option Id="S26" Time="08:30" Date="03-10-2013"/>
      </Level1>
    </Combination>
     .....
</Root>

是否可以使用 LINQ2XML 来获得它?或者为了得到我想要的日期和时间属性应该存在于组合节点中的顺序?

【问题讨论】:

    标签: c# linq-to-xml


    【解决方案1】:

    是的,可以使用 LINQ to XML 来完成,但是查询很可怕:

    // load XML into XDocument instance
    var xDoc = XDocument.Load("Input.txt");
    
    // create new XDocument using info from the loaded one
    var xDoc2 = new XDocument(
                    new XElement("Root",
                        xDoc.Root
                            .Elements("Combination")
                            .Select(c => new XElement("Combination",
                                            c.Attributes(),
                                            c.Elements("Level")
                                             .Select(l => new XElement("Level",
                                                              l.Attributes(),
                                                              l.Elements("Option")
                                                               .OrderBy(o => (DateTime)o.Attribute("Date") + TimeSpan.Parse((string)o.Attribute("Time")))))))
                            .OrderBy(c => c.Elements("Level")
                                           .First(l => (int)l.Attribute("Id") == 1)
                                           .Elements("Option")
                                           .Select(o => (DateTime)o.Attribute("Date") + TimeSpan.Parse((string)o.Attribute("Time")))
                                           .First())
    
                                           ));
    
    // save the new document
    xDoc2.Save("Input.txt");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      相关资源
      最近更新 更多