Click here to Skip to main content
13,589,943 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 11 May 2007

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


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


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 + ",";


        foreach (XmlNode vchildnode in vnode)

    while (vnode.HasChildNodes==false)
        string stringtest ="";
        foreach (XmlAttribute atttest1 in vnode.Attributes)
            stringtest = stringtest + atttest1.Value + ",";

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);


test2 = test.DocumentElement;

outwriter = new StreamWriter(outfile);



simple, but it does the trick


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.


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


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

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


You may also be interested in...

Comments and Discussions

GeneralError is coming during run time. Pin
sridharan2824-Mar-11 18:42
membersridharan2824-Mar-11 18:42 
GeneralThe Code has some problems Pin
nsingireddy4-Mar-11 6:44
membernsingireddy4-Mar-11 6:44 
GeneralXML to CSV converter (XSL free) Pin
bleekay2-Jun-10 8:00
memberbleekay2-Jun-10 8:00 
GeneralRe: XML to CSV converter (XSL free) Pin
bleekay11-Jun-10 8:18
memberbleekay11-Jun-10 8:18 
GeneralXSLT Pin
danielbromley13-May-07 23:38
memberdanielbromley13-May-07 23:38 
GeneralRe: XSLT Pin
daluu6-Jan-08 16:45
memberdaluu6-Jan-08 16:45 
GeneralXSLT Pin
Mark Nischalke11-May-07 7:36
mvpMark Nischalke11-May-07 7:36 
GeneralRe: XSLT Pin
Virtual Coder12-May-07 7:48
memberVirtual Coder12-May-07 7:48 
GeneralRe: XSLT Pin
daluu6-Jan-08 16:40
memberdaluu6-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180618.1 | Last Updated 11 May 2007
Article Copyright 2007 by danielbromley
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid