Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C#: Sort xml nodes and childnodes using attribute value

Referencing the two questions here and here
I would like to sort on child nodes as well, but resetting for each "outer" node.
So:

<Root>
    <I aa="1">
        <s aa="3"/>
        <s aa="1"/>
        <s aa="2"/>
    </I>
    <I aa="5">
        <s aa="3"/>
        <s aa="1"/>
        <s aa="2"/>
    </I>
    <I aa="3">
        <s aa="3"/>
        <s aa="1"/>
        <s aa="2"/>
    </I>
    <I aa="4">
        <s aa="3"/>
        <s aa="1"/>
        <s aa="2"/>
    </I>
</Root>

Would become:

<Root>
    <I aa="1">
        <s aa="1"/>
        <s aa="2"/>
        <s aa="3"/>
    </I>
    <I aa="3">
        <s aa="1"/>
        <s aa="2"/>
        <s aa="3"/>
    </I>
    <I aa="4">
        <s aa="1"/>
        <s aa="2"/>
        <s aa="3"/>
    </I>
    <I aa="5">
        <s aa="1"/>
        <s aa="2"/>
        <s aa="3"/>
    </I>
</Root>

I tried different variations of the XDocument sorting, but I cannot get the syntax right.
Hoping someone can help, or provide me with an idea.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need some recursion here – each element needs to be cloned and its child elements sorted by the aa attribute. A method to do that might look like this:

private static XElement CopyAndSort(XElement e)
{
    var elements = e.Elements()
        .OrderBy(x => (string)x.Attribute("aa"))
        .Select(CopyAndSort);
    return new XElement(e.Name, e.Attributes(), elements);
}

If you then call that with your Root element, you will get back a new Root element with all its children sorted (and their children sorted, and so on).

See this fiddle for a working demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading