Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting all the elements in a XDocument

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.

like image 939
Zero Cool Avatar asked Aug 12 '10 16:08

Zero Cool


3 Answers

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));
}
like image 66
dtb Avatar answered Nov 20 '22 03:11

dtb


THIS METHOD MAKES A REAL DOCUMENT EXTENSION AND PRESERVES THE ATTRIBUTES AND TEXT VALUES

I came up with this based off a few different posts and code from here and there... Thanks to all who contributed!

Within the same namespace (not the same class) add the following...

public static void Sort(this XElement source, bool bSortAttributes = true)
{
    //Make sure there is a valid source
    if (source == null) throw new ArgumentNullException("source");

    //Sort attributes if needed
    if (bSortAttributes)
    {
        List<XAttribute> sortedAttributes = source.Attributes().OrderBy(a => a.ToString()).ToList();
        sortedAttributes.ForEach(a => a.Remove());
        sortedAttributes.ForEach(a => source.Add(a));
    }

    //Sort the children IF any exist
    List<XElement> sortedChildren = source.Elements().OrderBy(e => e.Name.ToString()).ToList();
    if (source.HasElements)
    {
        source.RemoveNodes();
        sortedChildren.ForEach(c => c.Sort(bSortAttributes));
        sortedChildren.ForEach(c => source.Add(c));
    }
}

To use the document extension...

//Load the xDoc
XDocument xDoc = XDocument.Load("c:\test.xml");

//Sort the root element
xDoc.Root.Sort();
like image 43
Arvo Bowen Avatar answered Nov 20 '22 04:11

Arvo Bowen


Here is an updated example that will include all attributes when performing the sort.

private static XElement Sort(XElement element)
{
    XElement newElement = new XElement(element.Name,
        from child in element.Elements()
        orderby child.Name.ToString()
        select Sort(child));
    if (element.HasAttributes)
    {
        foreach (XAttribute attrib in element.Attributes())
        {
            newElement.SetAttributeValue(attrib.Name, attrib.Value);
        }
    }
    return newElement;
}

private static XDocument Sort(XDocument file)
{
    return new XDocument(Sort(file.Root));
}

This post helped me a lot, because I did not want to perform an XML sort using XSLT since I did not want to reformat the XML. I searched all around for a simple solution to perform an XML sort using C# and ASP.NET and I was delighted when I found this thread. Thanks to all, this did exactly what I needed.

like image 27
Matt Heidenreich Avatar answered Nov 20 '22 03:11

Matt Heidenreich