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 6970121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:40:10+00:00 2026-05-27T16:40:10+00:00

I have an XML that needs it’s elements rearranging. Is there a way to

  • 0

I have an XML that needs it’s elements rearranging. Is there a way to do this using LINQ?

as in the below example, elements 3 and 2 have been mixed up, and need rearranging so the elements are 1,2 and 3. likely the next time data comes in, it will be in an even different order, but will need putting back to 1,2 and 3.

<a>
  <1></1>
  <3></3>
  <2></2>
</a>
  • 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-27T16:40:11+00:00Added an answer on May 27, 2026 at 4:40 pm

    The hardest part is getting the correct order from the XSD. You can load the XSD as an XDocument and query it directly with XML.

    Consider the following schema based on your example (I added the underscores since 1, 2, & 3 are not valid XML names by themselves)

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="XMLSchema1"
        targetNamespace="http://tempuri.org/XMLSchema1.xsd"
        elementFormDefault="qualified"
        xmlns="http://tempuri.org/XMLSchema1.xsd"
        xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
    >
    
      <xs:complexType name="a">
        <xs:sequence>
          <xs:element name="_1" type="xs:string" minOccurs="1" maxOccurs="1" />
          <xs:element name="_2" type="xs:string" minOccurs="1" maxOccurs="1" />
          <xs:element name="_3" type="xs:string" minOccurs="1" maxOccurs="1" />
        </xs:sequence>
      </xs:complexType>
    </xs:schema>    `
    

    If you load this into an object (XDocument or XElement are best), you can do a LINQ query on the schema (the query is not tested, I am doing this from memory from the last time i did this, but if i am missing something, it should point you in the right direction)

    var schema = XDocument.Load(@"C:\somepath\XMLSchema1.xsd");
    var xs = XNamespace.Get(@"http://www.w3.org/2001/XMLSchema");
    
    var order = (from x in schema.Elements(xs + "schema").Elements(xs + "complexType") 
                 where x.Attributes("name").FirstOrDefault().Value == "a" 
                 select x.Element(xs + "sequence").Elements())
                .First()
                .Select(x => x.Attribute("name").Value);
    

    If you have a more complex XSD, with pre-defined types and/or custom name spaces, it will make the query to get the order of the elements more complex, but hopefully you get the idea. If you have an extremely complex XSD, see some of the links i have at the end of this answer for help understanding the XmlSchemaInfo object model and you might be abler to write your own query to get the order far more easily.

    Once you have the correct order, I don’t recommend using LINQ to actually change the order of the nodes as LINQ was not designed to modify objects, but rather using it to find the nodes and the parent node, then rearranging them in the order you want.

    XElement xelem = // get parent element of the elements you need to rearrange
    
    foreach (var element in order)
    {
        var node = xelem.Element(element);
        node.Remove();
        xelem.Add(node);                
    }
    

    if you are going to be working with accessing XSD directly in code frequently, I recommend reading the following questions and answers for help as the XmlSchemaInfo object model is not well documented, and will take some time & effort to get a workable method to extract what you need. It will be easier finding and extracting the information you need from the schema.

    XmlSchema inferred from an XML file – how to iterate through all the elements in the XSD? (Morawski’s question he reference in his comment)

    Capture Schema Information when validating XDocument (my own question & answer)

    In C#, how to determine the XSD-defined MaxLength for an element

    http://geekswithblogs.net/.NETonMyMind/archive/2006/05/02/76957.aspx (Great blog posting from where I got most of the info for my SchemaInfo question and answer)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a total beginner in JS/XML. I have this simple code that needs
I have an XML file that needs parsing in PHP. I am currenlty using
I have an XML that needs to be parsed using jQuery. I understand how
I have a service that needs to generate xml. Currently I am using jaxb
Is there a way to define an XML schema that allows elements not defined
I have an XML that needs to be databound to a WPF TreeView .
I have a large xml document that needs to be processed 100 records at
I have a list of xml elements that i need to search for specific
I have an xml file that I need to load. This xml file holds
I have program that needs to parse XML that contains character entities. The program

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.