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