Adding nodes to an existing xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Daljeet Hanspal
    New Member
    • Feb 2008
    • 6

    #1

    Adding nodes to an existing xml file

    hi ....
    i am trying to add a new node to an existing xml file in c# ..........
    the code is as follows:

    XmlDocument doc = new XmlDocument();
    string filename = @"C:\Documen ts and Settings\Admini strator\My Documents\Visua l Studio 2005\Projects\x ml\xml\bin\Debu g\bookstore.xml ";
    //create a new node
    XmlElement newbook = doc.CreateEleme nt("book");

    //set attribute
    newbook.SetAttr ibute(label1.Te xt, textBox1.Text);

    //create new element
    XmlElement npublication = doc.CreateEleme nt(label2.Text) ;
    npublication.In nerText = textBox2.Text;
    newbook.AppendC hild(npublicati on);

    XmlElement nisbn = doc.CreateEleme nt(label3.Text) ;
    nisbn.InnerText = textBox3.Text;
    newbook.AppendC hild(nisbn);

    XmlElement ntitle = doc.CreateEleme nt(label4.Text) ;
    ntitle.InnerTex t = textBox4.Text;
    newbook.AppendC hild(ntitle);

    XmlElement nname = doc.CreateEleme nt(groupBox2.Te xt);
    newbook.AppendC hild(nname);

    XmlElement fname = doc.CreateEleme nt(label5.Text) ;
    fname.InnerText = textBox5.Text;
    nname.AppendChi ld(fname);

    XmlElement lname = doc.CreateEleme nt(label6.Text) ;
    lname.InnerText = textBox6.Text;
    nname.AppendChi ld(lname);

    XmlElement nprice = doc.CreateEleme nt(label7.Text) ;
    nprice.InnerTex t = textBox7.Text;
    newbook.AppendC hild(nprice);

    XmlNode xn = doc.DocumentEle ment;
    xn.AppendChild( newbook);


    i get this message "Object reference not set to an instance of an object."

    please help me out
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    It sounds like your problem is with your language syntax, not XML. You should probably post your question in a form that supports what ever language you are using.

    Also you may want to google and review basic forum guidelines. Posts like these generally will not get much of a response. More specific information is needed. For instance when do you get that error? When compiling, at run time? It also looks like the code is incomplete. There are a number of objects that are not defined in the same provided, and there are other objects like filename that never seem to be used.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Could we see where you are declaring each of the labels, textboxes, etc??

      This really seems like something that should be caught in the compiler with a warning.

      Comment

      Working...