0

i added xml file in my windows application, i want to add values to that from textbox.. i used the following code,

string path = "codedata.xml";
        XmlDocument doc = new XmlDocument();
        if (!System.IO.File.Exists(path))
        {
            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            doc.AppendChild(declaration);
            doc.AppendChild(comment);
        }
        else //If there is already a file
        {
            //    //Load the XML File
            doc.Load(path);
        }

        //Get the root element
        XmlElement root = doc.DocumentElement;

        XmlElement Subroot = doc.CreateElement("data");
        XmlElement Companycode = doc.CreateElement("Companycode");
        XmlElement Productcode = doc.CreateElement("Productcode");
        XmlElement Productname = doc.CreateElement("Productname");
        XmlElement Brandcode = doc.CreateElement("Brandcode");
        XmlElement Brandname = doc.CreateElement("Brandname");

        Companycode.InnerText = txt_companycode.Text;
        Productcode.InnerText = txt_productcode.Text;
        Productname.InnerText = txt_productname.Text;
        Brandcode.InnerText = txt_brandcode.Text;
        Brandname.InnerText = txt_brandname.Text;

        Subroot.AppendChild(Companycode);
        Subroot.AppendChild(Productcode);
        Subroot.AppendChild(Productname);
        Subroot.AppendChild(Brandcode);
        Subroot.AppendChild(Brandname);
        root.AppendChild(Subroot);
        doc.AppendChild(root);


        //Save the document
        doc.Save(path);


        //Show confirmation message
        MessageBox.Show("Details  added Successfully");

it showing error near root.AppendChild(Subroot); can any one help me, wer i made mistake.

2
  • object reference not set to an instance... Commented Jul 26, 2012 at 7:09
  • LINQ to XML is really nice in such a situation :) You get this error because there is no instance of Subroot try add this line : Subroot sub = new Subroot(); before this lineSubroot.AppendChild(Companycode); and change all Subroot to sub Commented Jul 26, 2012 at 7:53

3 Answers 3

2

You can use Linq to XML, here is an example :

var xDoc = XElement.Load("FilePath");
if (xDoc == null)
   return;

var myNewElement = new XElement("ElementName"
   new XAttribute("AttributeName", value1),
   new XAttribute("AttributeName", value2)
   //And so on ...
);
xDoc.Add(myNewElement);
xDoc.Save("FilePath");
Sign up to request clarification or add additional context in comments.

Comments

1

The root is null. Try to add Root element when you create XML file.

 if (!System.IO.File.Exists(path))
        {
            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            doc.AppendChild(declaration);
            doc.AppendChild(comment);
            doc.AppendChild(doc.CreateElement("Root"));
        }

Or use LINQ-XML

string _file=@"c:\sample.xml";
XDocument doc;

if (!File.Exists(_file))
{
    doc = new XDocument();
    doc.Add(new XElement("Root"));
}
else
{
    doc = XDocument.Load(_file);
}

doc.Root.Add(
      new XElement("data",
                   new XElement("CompanyCode","C101"),
                   new XElement("ProductCode","P101")
            ) 
      );
doc.Save(_file);

4 Comments

hi, i used, it returns saved, but no data in xml file
You either have set physical path c:\file.xml or have a look at Debug/Bin folder if your app is winform or console.
hai, when i test using break point, it shows the xml file with data i saved.but if i see in codedata.xml,its empty. Where it is saving?
Try to set absolute path and see what happen?
0

In null XML DocumentElement is null. Try add Subroot to Document:

XmlElement root = doc.DocumentElement;

root.AppendChild(Subroot); doc.AppendChild(root);

// new code
doc.AppendChild(Subroot);

3 Comments

i tried ur idea, its shows the msg saved, but in xml file...no data is there empty only with <?xml version="1.0" encoding="utf-8" ?> i added my xml file by add new item
@chitra: I tested my code and its works correctly. check output xml.
should i add any element in xml, before saved btn

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.