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.
document.Element("Table")is returning an actual value egvar tableElement = document.Element("Table"); tableElement.Add(elem)