We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

XML reorder nodes

rketterer44
rketterer44 asked
on
Medium Priority
991 Views
Last Modified: 2013-12-17
Hi,

I have an XML document with following nodes

 
Document
    Record
        SectionA
        SectionB
        SectionC
     Record
Document



I have following child nodes withIn SectionA
      SectionA
            SectionA_G
            SectionA_GSpec
            SectionA_IEth
            SectionA_BD
            SectionA_CS
                 SectionA_CS has several childnodes
            SectionA_SP
                 SectionA_SP has several chidnodes
             SectionA_HP
             SectionA_R
       SectionA


I need to reorder SectionA as follows

      SectionA
            SectionA_CS
                 SectionA_CS has several childnodes
            SectionA_SP
                 SectionA_SP has several chidnodes
            SectionA_G
            SectionA_GSpec
            SectionA_IEth
            SectionA_HP
            SectionA_R
            SectionA_BD
       SectionA
is there a solution in C# for this problem?

Thanks,
Comment
Watch Question

Commented:

I have built an application based on the XML you provided.  The import concept to understand is the xsl:copy-of select which will select a node and all its child nodes.
Here is what my input XML (XmlFile2.xml) looks like:

<?xml version="1.0" encoding="utf-8" ?>
<Document>
<Record>
<SectionA>
<SectionA_G></SectionA_G>
<SectionA_GSpec></SectionA_GSpec>
<SectionA_IEth></SectionA_IEth>
<SectionA_BD></SectionA_BD>
<SectionA_CS>
<A_CS1></A_CS1>
<A_CS2></A_CS2>
</SectionA_CS>
<SectionA_SP>
<A_SP1></A_SP1>
<A_SP2></A_SP2>
<A_SP3></A_SP3>
</SectionA_SP>
<SectionA_HP></SectionA_HP>
<SectionA_R></SectionA_R>
</SectionA>
<SectionB>
</SectionB>
<SectionC>
</SectionC>
</Record>
</Document>  

I have created a C# windows form application which has a button on it (button4) which translates the input XML format to your output format using an XSLT file.  I have attached button4's source code as a snippet.  It is important to have the following 4 using statements in your code.  
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
The XSLT I am using to translate (XSLTFile2.xslt) is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Document>
<Record>
<SectionA>
<xsl:copy-of select="Document/Record/SectionA/SectionA_CS"/>
<xsl:copy-of select="Document/Record/SectionA/SectionA_SP"/>
<xsl:copy-of select="Document/Record/SectionA/SectionA_G"/>
<xsl:copy-of select="Document/Record/SectionA/SectionA_GSpec"/>
<xsl:copy-of select="Document/Record/SectionA/SectionA_IEth"/>
<xsl:copy-of select="Document/Record/SectionA/SectionA_HP"/>
<xsl:copy-of select="Document/Record/SectionA/SectionA_R"/>
<xsl:copy-of select="Document/Record/SectionA/SectionA_BD"/>
</SectionA>
<xsl:copy-of select="Document/Record/SectionB"/>
<xsl:copy-of select="Document/Record/SectionC"/>
</Record>
</Document>
</xsl:template>
</xsl:stylesheet>

The output in this example is xml written to c:\result.xml.  This is the contents of c:\result.xml after the transform:

<Document>
<Record>
<SectionA>
<SectionA_CS>
<A_CS1></A_CS1>
<A_CS2></A_CS2>
</SectionA_CS>
<SectionA_SP>
<A_SP1></A_SP1>
<A_SP2></A_SP2>
<A_SP3></A_SP3>
</SectionA_SP>
<SectionA_G></SectionA_G>
<SectionA_GSpec></SectionA_GSpec>
<SectionA_IEth></SectionA_IEth>
<SectionA_HP></SectionA_HP>
<SectionA_R></SectionA_R>
<SectionA_BD></SectionA_BD>
</SectionA>
<SectionB>
</SectionB>
<SectionC>
</SectionC>
</Record>
</Document>

private void button4_Click(object sender, System.EventArgs e)
{
   try
   {
     //load the Xml doc
     XPathDocument myXPathDoc = new XPathDocument
       (@"..\..\App_Data\XMLFile2.xml") ;
 
     XslTransform myXslTrans = new XslTransform() ;
            
    //load the Xsl 
    myXslTrans.Load(@"..\..\App_Data\XSLTFile2.xslt") ;
            
    //create the output stream (c:\result.xml)
    XmlTextWriter myWriter = new XmlTextWriter
     (@"c:\result.xml", null);
            
				//do the actual transform of Xml
				myXslTrans.Transform(myXPathDoc,null, myWriter);        
 
				myWriter.Close() ;
 
 
			}
			catch(Exception ex)
			{
 
				Console.WriteLine("Exception: {0}", ex.ToString());
			}
 
 
		}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Was my solution understandable?  Did it do what you needed?  Any additional questions?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.