C# LINQ-TO-XML按属性排序XML不在顶层
我有这样一个xml文件:C# LINQ-TO-XML按属性排序XML不在顶层,c#,linq-to-xml,C#,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
<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的级别节点内的一些选项节点中的最早值排序(因此组合节点的顺序会发生变化):
.....
是否可以使用LINQ2XML获取此信息?或者要获得我想要的顺序,日期和时间属性应该存在于组合节点中 是的,可以使用LINQ to XML进行查询,但是,查询非常可怕:
//将XML加载到XDocument实例中
var xDoc=XDocument.Load(“Input.txt”);
//使用加载文档中的信息创建新XDocument
var xDoc2=新的XDocument(
新的XElement(“根”,
根
.要素(“组合”)
.选择(c=>新元素(“组合”,
c、 属性(),
c、 元素(“级别”)
.选择(l=>新元素(“级别”,
l、 属性(),
l、 要素(“期权”)
.OrderBy(o=>(DateTime)o.Attribute(“Date”)+TimeSpan.Parse((string)o.Attribute(“Time”;));)
.OrderBy(c=>c.Elements(“级别”)
.First(l=>(int)l.Attribute(“Id”)==1)
.要素(“选择权”)
.Select(o=>(DateTime)o.Attribute(“日期”)+TimeSpan.Parse((字符串)o.Attribute(“时间”))
.First())
));
//保存新文档
xDoc2.Save(“Input.txt”);
<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>