Click here to Skip to main content
Click here to Skip to main content

an XML to CSV converter

, 11 May 2007
Rate this:
Please Sign up or sign in to vote.
this is a quick simple bit of code in a project that can convert an XML file to a csv file

Introduction

its basically code for a very simple Nth level XML to CSV converter.This will convert a file full of XML of the following format

-<structure name="systemBoard" caption="Board 0">
    -<property name="memDevice0_0" value="512 Mbytes (400 MHz)" />
</structure> 

into a CSV file full of the details in the following format

systemboard,Board 0
memDevice0_0,512 Mbytes (400 MHz) 

and it should convert the file to whatever node child level it goes to

Background

The whole idea came from a simple XML conversion request from a work friend who was very stuck. Basically he sold a contract to a customer and nodded and smiled and said yes to everything without thinking of the work required to fulfil the contract. So when the time came to report to the customer on the contract he got an XML file from the customer and wet himself because he had no clue of what to do with it. It makes me laugh every time i see him. Anyway i thought i would share this because i have not seen anything like it anywhere and i bet someone somewhere will find it useful.

Using the code

The Heart of the code is the following bit of code

        /// <summary>
        /// N th lever recursion subprocedure
        /// </summary>
        private void getsubnode(XmlNode vnode,StreamWriter outfilewriter)
        {
            while (vnode.HasChildNodes==true)
            {
                string stringtest ="";
                foreach (XmlAttribute atttest1 in vnode.Attributes)
                {
                    stringtest = stringtest + atttest1.Value + ","; 
                }
                
                    outfilewriter.WriteLine(stringtest);

                foreach (XmlNode vchildnode in vnode)
                {
                    getsubnode(vchildnode,outfilewriter);
                }
                return;
            }
        
            while (vnode.HasChildNodes==false)
            {
                string stringtest ="";
                foreach (XmlAttribute atttest1 in vnode.Attributes)
                {
                    stringtest = stringtest + atttest1.Value + ","; 
                }
                outfilewriter.WriteLine(stringtest);
                return;
            }
        }

it will recursively dive through the XML file you pass it and work its way to whichever node depth the file has and convert each line in turn to the CSV format and dump it out your output file stream . It does this by itterating through each node outputting the attributes at that level and then calling itself with the next childnode. when there isnt another childnode level the code outputs the attributes at that base level then returns to the previous itteration to procede to the next node/attribute layer untill there are no more levels or nodes left.

this is all started by the GO button on the form, once you have a source file path
the code loads in the XML file using XmlDocument.Load then creates you output file stream to hold the outdoing data

        private void button2_Click(object sender, System.EventArgs e)
        {
        XmlDocument test = new XmlDocument();
        XmlNode test2;
        StreamWriter outwriter;
        FileStream outfile = new FileStream(textBox1.Text + ".out",FileMode.OpenOrCreate);

        test.Load(textBox1.Text);

        test2 = test.DocumentElement;

        outwriter = new StreamWriter(outfile);
            
            getsubnode(test2,outwriter);

        outwriter.Close();
        outfile.Close();
        } 

simple, but it does the trick

*8o)

Points of Interest

i know this is simple version of the code but its just here to demonstrate the principle. the output format and IO can (and probably will) be made much more complicated should you need it. I hope people find it interesting and that others find a use for this.

History

no further update planned for now, but all comments/suggestions are welcome

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

danielbromley

United Kingdom United Kingdom
Adrenalin and Caffeine fuelled DBA and Code Monkey
 
*8o)

Comments and Discussions

 
GeneralError is coming during run time. Pinmembersridharan2824-Mar-11 18:42 
GeneralThe Code has some problems Pinmembernsingireddy4-Mar-11 6:44 
GeneralXML to CSV converter (XSL free) Pinmemberbleekay2-Jun-10 8:00 
GeneralRe: XML to CSV converter (XSL free) Pinmemberbleekay11-Jun-10 8:18 
GeneralXSLT Pinmemberdanielbromley13-May-07 23:38 
GeneralRe: XSLT Pinmemberdaluu6-Jan-08 16:45 
GeneralXSLT PinmvpMark Nischalke11-May-07 7:36 
GeneralRe: XSLT PinmemberVirtual Coder12-May-07 7:48 
GeneralRe: XSLT Pinmemberdaluu6-Jan-08 16:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 11 May 2007
Article Copyright 2007 by danielbromley
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid