Object reference not set to an instance of an object. on XmlDocument Object

Jan 1 2004 12:10 PM
Hi All, I'm using the XmlDocument object to create a new XML document from scratch. In my code I declare the document object at the beginning of the class: XmlDocument doc; then I initialize it in a Form_Load Event handler: doc = new XmlDocument(); doc.CreateXmlDeclaration("1.0", "ASCII", string.Empty); doc.NodeChanged += new XmlNodeChangedEventHandler(this.MyNodeChangedEvent); doc.NodeInserted += new XmlNodeChangedEventHandler(this.MyXmlNodeInsertedEventHandler); doc.NodeRemoved += new XmlNodeChangedEventHandler(this.MyXmlNodeRemovedEventHandler); // Create the root node rootNode = doc.CreateNode(XmlNodeType.Element, "STATUSREPORT", string.Empty); newLineNode = doc.CreateTextNode("\n"); However, when I try to add an element to this document before saving it I get the following error : "Object reference not set to an instance of an object." The code I use to write the element is: XmlNode myNode = doc.CreateNode(XmlNodeType.Element, txtElementName.Text, null); myNode.InnerText = txtElementValue.Text; rootNode.AppendChild(myNode); This works if I do a doc.Save first but I don't think forcing the application to actually save an xml file is the way to go here. does anyone know what I'm doing wrong? thanks in advance, Leo

Answers (1)