How To Check if an Object is Instantiated?

Alabamas

New Member
I'm trying to list down all elements and attributes of an xml into two separate List objects.I was able to get all the elements in the xml.
But when I tried to add the functionality to get all the attributes within each element, I always encounter \[code\]System.NullReferenceException: Object reference not set to an instance of an object.\[/code\]
BXnZ0.png
Please review my code below and advise where I'm not doing it right. Or is there any better way to accomplish this? Your comments and suggestions will be highly appreciated.\[code\]using System;using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;using System.Xml;using System.IO;namespace TestGetElementsAndAttributes{ public partial class MainForm : Form { List<string> _elementsCollection = new List<string>(); List<string> _attributeCollection = new List<string>(); public MainForm() { InitializeComponent(); XmlDataDocument xmldoc = new XmlDataDocument(); FileStream fs = new FileStream(@"C:\Test.xml", FileMode.Open, FileAccess.Read); xmldoc.Load(fs); XmlNode xmlnode = xmldoc.ChildNodes[1]; AddNode(xmlnode); } private void AddNode(XmlNode inXmlNode) { try { if(inXmlNode.HasChildNodes) { foreach (XmlNode childNode in inXmlNode.ChildNodes) { foreach(XmlAttribute attrib in childNode.Attributes) { _attributeCollection.Add(attrib.Name); } AddNode(childNode); } } else { _elementsCollection.Add(inXmlNode.ParentNode.Name); } } catch(Exception ex) { MessageBox.Show(ex.GetBaseException().ToString()); } } }}\[/code\]Posting as well the sample XML.\[code\]<?xml version="1.0" encoding="UTF-8" ?> <DocumentName1> <Product> <Material_Number>21004903</Material_Number> <Description lang="EN">LYNX GIFT MUSIC 2012 1X3 UNITS</Description> <Packaging_Material type="25">457</Packaging_Material> </Product></DocumentName1>\[/code\]
 
Back
Top