Hi, looking for some troubleshooting assistance in the below code please. I keep getting the error: NullReferenceException was unhandled: Object reference not set to an instance of an object
Been scratching my head for days, any ideas? Thanks!
(I've attached the sample XML file - saved as .txt so I could upload)
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace XML_to_CSV_test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// N th lever recursion subfunction
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;
}
}
// Browse dialog button
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
txtBoxSourceFilePath.Text = openFileDialog1.FileName;
}
// Convert button
private void btnConvert_Click(object sender, EventArgs e)
{
XmlDocument test = new XmlDocument();
XmlNode test2;
StreamWriter outwriter;
FileStream outfile = new FileStream(txtBoxSourceFilePath.Text + ".csv", FileMode.OpenOrCreate);
test.Load(txtBoxSourceFilePath.Text);
test2 = test.DocumentElement;
outwriter = new StreamWriter(outfile);
getsubnode(test2, outwriter);
outwriter.Close();
outfile.Close();
}
}
}
Last edited by saldous; February 23rd, 2011 at 12:16 AM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.