3

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.

1
  • Looks like a basic NullRef exception. Check that document.Element("Table") is returning an actual value eg var tableElement = document.Element("Table"); tableElement.Add(elem) Commented Dec 17, 2013 at 22:24

4 Answers 4

6

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Please explain I'm Learning XML through C# Can you Please read the code once again
@user3113029 I added some details
how would you create the following code thorugh C# <db name="something"> <table1 name="something"> <column1 someattribuename="something" > Data </column> </table> </DB> first we add some root by xelement elem = new xelement("DB" , new Xattribute("Name", someinput"); Then what Im confuse with adding properly new nodes and modify exsiting nodes Thanks!
What are you confused about? See what I added to my question you should find your answer there.
I got it but if I want to add new element inside this "elem" then what should I do?
|
4

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.

Comments

0

Throw exception, because at the time the item is inserted in the XDocument(document) XElement (elem) element Element("Table") does not exist yet

1 Comment

Means I'm not inserting anything ??
0

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);

4 Comments

First document.Root.Add(elem); then docemnt.Element("Column", someattribute..) isn't so??
No, add it to the appropriate parent. You're tryting to add the table1 element that you're creating to an existing table1 element that apparently doesn't exist.
if I want to add some child in an existing node then what would you prefer?
It's more efficient to build up the entire tree then add it to the document - you're just trying to add it to a location that doesn't exist yet because you haven't added it yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.