Hi,
I have a XDocument where I'd like to sort all of the elements alphabetically. Here's a simplified version of the structure:
<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>
I'm wanting to sort the elements in this documents at all levels, so far I'm able to sort it like so:
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));
}
Which produces:
<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>
I'd like to be able to sort all of the children elements in the same way (through a recursive function ideally). Any ideas how I can get this going with LINQ?
Thanks for any ideas.