简体   繁体   中英

object reference not set to an instance of an object xml C# and adding some node in xml file using C#

this is simple code for using xml in C#. I want to add data like

<table1 name="something">
   <column1 someattribute="something"> actualname </column>
</table1>

then I want to add this element into XDocument's Object

XDocument document;
public Form1()
{
   InitializeComponent();
   document = new XDocument();
}

private void button1_Click(object sender, EventArgs e)
{
  //document = new XDocument();
   XElement elem = new XElement("table1");
   elem.Add(new XAttribute("TableName", textBox1.Text));
   elem.Add(new XElement("Column1",new XAttribute("Someattribute", somevalue));
   document.Element("table1").Add(elem);//throws exception object refernce not set ..."
}

I have tried the following code Adding elements to an xml file in C# but it throws an exception of "object reference not set to an instance of an object xml C#" Please help PS: suppose there are many columns in table1 then first I want to accmulate each column then add it to xdocument and then there is table2 and I want to do the same thing with him.

There are no table1 elements in your XML structure. This means that your document.Element("table1") table returns null , and therefore there is an exception when you call .Add(elem) on it.

Debugging NullReferenceExceptions is very basic most of the time and these problems can easily be solved simply by stepping through the code with a debugger.

For reference, the Element method

Gets the first (in document order) child element with the specified XName.

Which is why you get a null . ( MSDN )

When you initialize the form, you create an empty document like so new XDocument() . Then, in your button click, you are trying to add a new element as a child to table1 by using the selector document.Element("table1") . That is the issue, your XDocument is empty! You would need to first create a table1 element or instead add your elem object directly to the document.

Meaning you can either do this to make sure table1 exists:

document = XDocument.Parse("<table1></table1>);

or change your click method to add directory to the root of the document:

private void button1_Click(object sender, EventArgs e)
{
  //document = new XDocument();
   XElement elem = new XElement("table1");
   elem.Add(new XAttribute("TableName", textBox1.Text));
   elem.Add(new XElement("Column1",new XAttribute("Someattribute", somevalue)));
   document.Add(elem);
}

Every XML document/structure has to have one unique node/tag, called the root node, that should exist only once in every document. So a proper xml can look like this - one root, but many nodes:

<?xml version="1.0" encoding="UTF-8"?>
<root name="example">
  <node attribute="1">information 1</node>
  <node attribute="2">information 2</node>
</root>

There are two possibilities to access the root element using LINQ2XML: XDocument.Root and XDocument.Element("elementName") . Knowing this and using the given xml code, it should be easy to follow the following example, that uses LINQ2XML to manipulate the xml code:

// every XML structure has to have a root node, that is unique
// and should exists only once!
var xmlCode = @"<root name=""example"">
                    <node attribute=""1"">information 1</node>
                    <node attribute=""2"">information 2</node>
                </root>";
// load the example xml code
var document = XDocument.Parse(xmlCode);
// create new attribute object
var newAttribute = new XAttribute("attribute", "3");
// create new node / column object with the newly created attribute and set the content of the node
var newNode = new XElement("node", newAttribute, "information 3");
// add the newly created node to the XML Document root
// in LINQ2XML the root node can be accessed via the Root property
// of an XDocument object
document.Root.Add(newNode);
// alternative: find the document root node using its name, in this example the node 
// is named "root" and add the new element to it!
// document.Element("root").Add(newNode);

The output is:

<root name="example">
  <node attribute="1">information 1</node>
  <node attribute="2">information 2</node>
  <node attribute="3">information 3</node>
</root>

In your question table1 is root in this xml code and column1 equivalent is node .

If you are going to use LINQ2XML, than have a look at all methods that the framework offers.

抛出异常,因为在项目插入XDocument(文档)XElement(elem)元素时元素(“表”)尚不存在

You are trying to add your now element to an existing element that doesn't exist:

document.Element("table1").Add(elem);//throws exception object refernce not set ..."

The table1 element hasn't been added to the document yet. You need to reference the appropriate parent element. To add it to the root element use

document.Root.Add(elem);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM