Reputation: 115
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 P.S: 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.
Upvotes: 3
Views: 10456
Reputation: 7830
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.
Upvotes: 4
Reputation: 24088
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);
}
Upvotes: 6
Reputation: 152624
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);
Upvotes: 0
Reputation: 606
Throw exception, because at the time the item is inserted in the XDocument(document) XElement (elem) element Element("Table") does not exist yet
Upvotes: 0