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.
You already have a method to sort the elements. Just apply it recursively:
private static XElement Sort(XElement element)
{
return new XElement(element.Name,
from child in element.Elements()
orderby child.Name.ToString()
select Sort(child));
}
private static XDocument Sort(XDocument file)
{
return new XDocument(Sort(file.Root));
}
Note that this strips all non-element nodes (attributes, text, comments, etc.) from your document.
If you want to keep the non-element nodes, you have to copy them over:
private static XElement Sort(XElement element)
{
return new XElement(element.Name,
element.Attributes(),
from child in element.Nodes()
where child.NodeType != XmlNodeType.Element
select child,
from child in element.Elements()
orderby child.Name.ToString()
select Sort(child));
}
private static XDocument Sort(XDocument file)
{
return new XDocument(
file.Declaration,
from child in file.Nodes()
where child.NodeType != XmlNodeType.Element
select child,
Sort(file.Root));
}