CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  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.

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