0

我有一个这样的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 来获得它?或者为了得到我想要的日期和时间属性应该存在于组合节点中的顺序?

4

1 回答 1

0

是的,它可以使用 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");
于 2013-10-08T15:01:01.023 回答