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>

そして、ノード Option の日付と時刻の属性で並べ替えたい (各オプションはそれ自体を並べ替えた) だけでなく、ID=1 のレベル ノード内のいくつかの Option ノードで最も早い値で並べ替えます (したがって、組み合わせノードの順序が変わります)。 ):

<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 に答える