Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4230790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:54:07+00:00 2026-05-21T01:54:07+00:00

I want to sort a XML file with LINQ. The XML is below and

  • 0

I want to sort a XML file with LINQ. The XML is below and is only an example. Normally it is much bigger and more complex. The XML should be sorted by title, ascending. Not the whole XML at once, but every parentNode for itself. Leafs are always at the bottom. Folder or non-leafs are at the top. The following XML is already well structured, but the titles are in the wrong order. The sort algorithm should also replace wrong positioned non-leaf nodes.
I have already some code which does the job, but I want to know if there is a more elegant or a shorter way. At the moment I have to call a function recursive to make the traversion. Maybe this can be done in another way.
Thank you.

Rene

Here is my XML:

<Node title="text99" leaf="no">
<Node title="text98" leaf="no">
    <Node title="text97" leaf="no">
        <Node title="text96" leaf="yes"/>
        <Node title="text95" leaf="yes"/>
    </Node>
    <Node title="text94" leaf="no">
        <Node title="text93" leaf="yes"/>
        <Node title="text92" leaf="yes"/>
    </Node>
    <Node title="text91" leaf="yes"/>
    <Node title="text90" leaf="yes"/>
</Node>
<Node title="text89" leaf="no">
    <Node title="text88" leaf="no">
        <Node title="text87" leaf="yes"/>
        <Node title="text86" leaf="yes"/>
    </Node>
    <Node title="text85" leaf="no">
        <Node title="text84" leaf="yes"/>
        <Node title="text83" leaf="yes"/>
    </Node>
    <Node title="text82" leaf="yes"/>
    <Node title="text81" leaf="yes"/>
</Node>
<Node title="text80" leaf="no">
    <Node title="text79" leaf="no">
        <Node title="text78" leaf="no">
            <Node title="text78" leaf="yes"/>
            <Node title="text77" leaf="yes"/>
        </Node>
        <Node title="text76" leaf="no">
            <Node title="text75" leaf="yes"/>
            <Node title="text74" leaf="yes"/>
        </Node>
        <Node title="text73" leaf="yes"/>
        <Node title="text72" leaf="yes"/>
    </Node>
    <Node title="text71" leaf="no">
        <Node title="text70" leaf="no">
            <Node title="text69" leaf="yes"/>
            <Node title="text68" leaf="yes"/>
        </Node>
        <Node title="text67" leaf="no">
            <Node title="text66" leaf="yes"/>
        </Node>
        <Node title="text65" leaf="yes"/>
        <Node title="text64" leaf="yes"/>
    </Node>
    <Node title="text63" leaf="yes"/>
    <Node title="text62" leaf="yes"/>
</Node>
<Node title="text61" leaf="yes"/>
<Node title="text60" leaf="yes"/>

And here is my code:

using (XmlReader reader = XmlReader.Create(XmlStream))
{
    XDocument xDoc = XDocument.Load(reader);                        
    Action<XElement> sortXml = null;
    sortXml = xElement =>
    {
    bool sortParentNode = false;
    foreach (var xElem in xElement.Elements())
    {                                    
        if (xElem.HasElements)
        {
            // go into deep
                sortXml(xElem);
        }
        else
            {
        // break loop and sort parentNode
        sortParentNode = true;
        break;
        }                                    
    }
    if (sortParentNode)
    {
        xElement.ReplaceNodes(from node in xElement.Elements()
                orderby node.Attribute("title").Value
                group node by node.HasElements into folderGroup
                orderby folderGroup.Key descending
                select folderGroup);
    }
};
sortXml(xDoc.Root);                        
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T01:54:07+00:00Added an answer on May 21, 2026 at 1:54 am

    The XML should be sorted by title,
    ascending. Not the whole XML at once,
    but every parentNode for itself. Leafs
    are always at the bottom. Folder or
    non-leafs are at the top.

    This solution seems to fit your requirements:

    public static void SortXml(XElement node)
    {
        node.ReplaceNodes(node.Elements("Node")
            .OrderBy(x => (string)x.Attribute("leaf"))
            .ThenBy(x => (string)x.Attribute("title")));
    
        foreach (var childNode in node.Elements("Node"))
            SortXml(childNode);
    }
    
    ...
    
    XDocument doc = XDocument.Load("test.xml");
    SortXml(doc.Root);
    

    All child nodes are sorted by the value of the leaf attribute first (uses the fact that “no” comes before “yes” in alphabetical order), and by title secondary. All first level child nodes are sorted in this fashion, then repeat recursively using each of those child nodes as input.

    Output:

    <Node title="text99" leaf="no">
      <Node title="text80" leaf="no">
        <Node title="text71" leaf="no">
          <Node title="text67" leaf="no">
            <Node title="text66" leaf="yes" />
          </Node>
          <Node title="text70" leaf="no">
            <Node title="text68" leaf="yes" />
            <Node title="text69" leaf="yes" />
          </Node>
          <Node title="text64" leaf="yes" />
          <Node title="text65" leaf="yes" />
        </Node>
        <Node title="text79" leaf="no">
          <Node title="text76" leaf="no">
            <Node title="text74" leaf="yes" />
            <Node title="text75" leaf="yes" />
          </Node>
          <Node title="text78" leaf="no">
            <Node title="text77" leaf="yes" />
            <Node title="text78" leaf="yes" />
          </Node>
          <Node title="text72" leaf="yes" />
          <Node title="text73" leaf="yes" />
        </Node>
        <Node title="text62" leaf="yes" />
        <Node title="text63" leaf="yes" />
      </Node>
      <Node title="text89" leaf="no">
        <Node title="text85" leaf="no">
          <Node title="text83" leaf="yes" />
          <Node title="text84" leaf="yes" />
        </Node>
        <Node title="text88" leaf="no">
          <Node title="text86" leaf="yes" />
          <Node title="text87" leaf="yes" />
        </Node>
        <Node title="text81" leaf="yes" />
        <Node title="text82" leaf="yes" />
      </Node>
      <Node title="text98" leaf="no">
        <Node title="text94" leaf="no">
          <Node title="text92" leaf="yes" />
          <Node title="text93" leaf="yes" />
        </Node>
        <Node title="text97" leaf="no">
          <Node title="text95" leaf="yes" />
          <Node title="text96" leaf="yes" />
        </Node>
        <Node title="text90" leaf="yes" />
        <Node title="text91" leaf="yes" />
      </Node>
      <Node title="text60" leaf="yes" />
      <Node title="text61" leaf="yes" />
    </Node>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that I want to sort by an attribute. The
I have an XML file whose contents I want to sort by document order
I have an input file that I want to sort based on timestamp which
I have a huge tab separated file which I want to sort on its
I have a file temp.txt, that I want to sort with the sort command
I'm trying to persist an XML file to disk using LINQ. I have a
I'm trying to sort a bunch of records in an XML file. The trick
I have a linked list that I want to sort part of, eg: std::sort(someIterator,
I have a list of Python objects that I want to sort by a
Given a list of strings, I want to sort it alphabetically and remove duplicates.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.