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.
Hi, thanks for the reply, sorry I forgot to include that info. It errors out here:
Code:
while (vnode.HasChildNodes == false)
{
string stringtest2 = "";
foreach (XmlAttribute atttest1 in vnode.Attributes)
The call stack shows:
XML to CSV test2.exe!XML_to_CSV_test2.Form1.getsubnode(System.Xml.XmlNode vnode = {unknown}, System.IO.StreamWriter outfilewriter = {unknown})
XML to CSV test2.exe!XML_to_CSV_test2.Form1.getsubnode(System.Xml.XmlNode vnode = {unknown}, System.IO.StreamWriter outfilewriter = {unknown})
XML to CSV test2.exe!XML_to_CSV_test2.Form1.getsubnode(System.Xml.XmlNode vnode = {unknown}, System.IO.StreamWriter outfilewriter = {unknown})
XML to CSV test2.exe!XML_to_CSV_test2.Form1.btnConvert_Click(object sender = {unknown}, System.EventArgs e = {unknown})
----
- Thrown: "Object reference not set to an instance of an object." (System.NullReferenceException) Exception Message = "Object reference not set to an instance of an object.", Exception Type = "System.NullReferenceException"
Exception Message "Object reference not set to an instance of an object." string
Exception Type "System.NullReferenceException" string
- Stopped at Exception vnode.Attributes = null, vnode = {Text, Value="7.0"}
vnode.Attributes null
vnode {Text, Value="7.0"}
----
By the way, the reason I'm not using XSLT is because my application pulls 12 different XML reports from our systems API interface, each report has different data sets, and sometimes we have people creating custom reports which then would need new transforms. I didn't want to have to build transforms for 12 different reports and then leave out the custom ones, so I was looking to do it this way instead. (unfortunately I have no control over the XML format our systems API spits out)
Last edited by saldous; February 23rd, 2011 at 08:51 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.