CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    34

    XML to CSV, getting error

    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(); 
    
        }
      }
    }
    Attached Files Attached Files
    Last edited by saldous; February 23rd, 2011 at 12:16 AM.

  2. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: XML to CSV, getting error

    When you get the error you should also get a stack trace which indicates what line the exception was thrown from.

    Without the trace/line numbers I can only venture a guess (without actually implementing the code), but my eyes goes to the:
    Code:
    XmlNode test2;
    ...
    test.Load(txtBoxSourceFilePath.Text);
    lines as the likely culprit.

    If not those being the issue verify your outfilewriter is not null and similar.

    But advice - check the stacktrace and the linenumber of the exception thrown.

  3. #3
    Join Date
    Nov 2009
    Posts
    34

    Re: XML to CSV, getting error

    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.

  4. #4
    Join Date
    Nov 2009
    Posts
    34

    Re: XML to CSV, getting error

    I'm still looking for help on this. Been scratching my head for weeks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured