2013-10-08 2 views
0

У меня есть XML-файл, как это:LINQ-TO-XML Заказ 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 (каждый вариант заказал сам по себе), но и по заказу первой величины в некоторых опциональных узлов внутри узла уровня с идентификатором = 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? Или, чтобы получить заказ, я хочу, чтобы атрибуты даты и времени существовали в Комбинированных узлах?

ответ

0

Да, это может быть сделано с помощью LINQ к 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"); 
Смежные вопросы