Click here to Skip to main content
15,878,852 members
Articles / Programming Languages / XML
Article

an XML to CSV converter

Rate me:
Please Sign up or sign in to vote.
2.23/5 (7 votes)
11 May 20072 min read 84K   2.6K   25   9
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"><br /> -<property name="memDevice0_0" value="512 Mbytes (400 MHz)" /><br /></structure>

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

systemboard,Board 0<br />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

C#
/// <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

C#
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


Written By
United Kingdom United Kingdom
Adrenalin and Caffeine fuelled DBA and Code Monkey

*8o)

Comments and Discussions

 
GeneralError is coming during run time. Pin
sridharan2824-Mar-11 18:42
sridharan2824-Mar-11 18:42 
GeneralThe Code has some problems Pin
nsingireddy4-Mar-11 6:44
nsingireddy4-Mar-11 6:44 
GeneralXML to CSV converter (XSL free) Pin
bleekay2-Jun-10 8:00
bleekay2-Jun-10 8:00 
GeneralRe: XML to CSV converter (XSL free) Pin
bleekay11-Jun-10 8:18
bleekay11-Jun-10 8:18 
GeneralXSLT Pin
danielbromley13-May-07 23:38
danielbromley13-May-07 23:38 
GeneralRe: XSLT Pin
daluu6-Jan-08 16:45
daluu6-Jan-08 16:45 
True but XSLT has more to offer:

* Platform agnostic. Create an XSLT stylesheet and run it with any XSLT parser (of your platform choice) against the XML input. No need to create custom function, library, or binary.

* Standard conversion and data format. Anyone who knows XSLT can modify or extend the XML to CSV conversion as needed. No need to read over such an article or the code (or its documentation) before modifying.

"A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible." - Freeman Dyson

GeneralXSLT Pin
Not Active11-May-07 7:36
mentorNot Active11-May-07 7:36 
GeneralRe: XSLT Pin
Virtual Coder12-May-07 7:48
Virtual Coder12-May-07 7:48 
GeneralRe: XSLT Pin
daluu6-Jan-08 16:40
daluu6-Jan-08 16:40 

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

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