我有一个XDocument,我想按字母顺序对所有元素进行排序.这是结构的简化版本:
<Config>
<Server>
<Id>svr1</Id>
<Routing>
<RoutingNodeName>route1</RoutingNodeName>
<Subscription>
<Id>1</Id>
</Subscription>
<RoutingParameters id="Routing1">
<Timeout>7200</Timeout>
</RoutingParameters>
</Routing>
<Storage>
<Physical>HD1</Physical>
</Storage>
</Server>
<Applications>
<Services>
<Local></Local>
</Services>
</Applications>
</Config>
Run Code Online (Sandbox Code Playgroud)
我想在各个级别对这些文档中的元素进行排序,到目前为止我能够像这样对它进行排序:
private static XDocument Sort(XDocument file)
{
return new XDocument(
new XElement(file.Root.Name,
from el in file.Root.Elements()
orderby el.Name.ToString()
select el));
}
Run Code Online (Sandbox Code Playgroud)
哪个产生:
<Config>
<Applications>
<Services>
<Local></Local>
</Services>
</Applications>
<Server>
<Id>svr1</Id>
<Routing>
<RoutingNodeName>route1</RoutingNodeName>
<Subscription>
<Id>1</Id>
</Subscription>
<RoutingParameters id="Routing1">
<Timeout>7200</Timeout>
</RoutingParameters>
</Routing>
<Storage>
<Physical>HD1</Physical>
</Storage>
</Server>
</Config>
Run Code Online (Sandbox Code Playgroud)
我希望能够以相同的方式对所有子元素进行排序(理想情况下通过递归函数).任何想法我怎么能用LINQ得到这个?
谢谢你的任何想法.
有谁知道我可以加载XML文件并对其进行排序然后保存文件的方式?
我有一个带有一堆设置的xml文件..现在它变得难以管理,因为它们没有任何自然的排序顺序......
例如
<edit_screen_a>
<settings_font_size>
<edit_screen_b>
<display_screen>
<settings_font_name>
Run Code Online (Sandbox Code Playgroud)
排序:
<display_screen>
<edit_screen_a>
<edit_screen_b>
<settings_font_name>
<settings_font_size>
Run Code Online (Sandbox Code Playgroud)