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

  • SEARCH
  • Home
  • 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 984187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:58:29+00:00 2026-05-16T04:58:29+00:00

I have a XDocument where I’d like to sort all of the elements alphabetically.

  • 0

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.

  • 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-16T04:58:30+00:00Added an answer on May 16, 2026 at 4:58 am

    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));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XDocument with a number of elements/child elements etc and I'd like
I have a XDocument that looks like this: XDocument outputDocument = new XDocument( new
Hai I have An XDocument .All nodes in this document have an attribute UserId.
I have a loaded XDocument that I need to grab all the attributes that
I have an existing XDocument object that I would like to add an XML
I have an XDocument like this one set as a DataContext of my Window
I have an embedded XML as Resource. When trying to load it like: XDocument
I have an XDocument object. I want to query for elements with a particular
Is there any way to have an XDocument print the xml version when using
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we

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.