?
Solved

XML reorder nodes

Posted on 2009-02-13
3
Medium Priority
?
959 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,
0
Comment
Question by:rketterer44
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
Jammer59 earned 2000 total points
ID: 23637723

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

0
 
LVL 6

Expert Comment

by:Jammer59
ID: 23683363
Was my solution understandable?  Did it do what you needed?  Any additional questions?
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question