none
Append to existing xml file

    Question

  • Hi.

    What is the best way to append a new node/element to an existing xml file?

    currently reading the entire doc into a filestream, then using xmltextwriter to write the new element but of course it results in an invalid xml file because it does not set the rootnode to end at the end of the final node/element inserted into the xml file

     

    thanks!

    Wednesday, November 09, 2005 7:51 AM

Answers

  • Being an hierarchy-based XML document can't be easily updated in place. The easiest and the least effective way is to load the whole document into memory, add node and save it back overwriting file on disc. Another more perf-friendly, but more complex solution is to read XML with XmlReader and write it simultaneously to XmlWriter, adding new nodes when needed. This way you can't obviously write to teh same file - you can create temporary one and then replace existing one.
    Completely different approach is to store XML fragment instead of XML document. This is the best for logging-like applications, when you need to append new element to the end of file with no parsing of existing part. See http://www.tkachenko.com/blog/archives/000053.html
    Wednesday, November 09, 2005 9:45 AM

All replies

  • Being an hierarchy-based XML document can't be easily updated in place. The easiest and the least effective way is to load the whole document into memory, add node and save it back overwriting file on disc. Another more perf-friendly, but more complex solution is to read XML with XmlReader and write it simultaneously to XmlWriter, adding new nodes when needed. This way you can't obviously write to teh same file - you can create temporary one and then replace existing one.
    Completely different approach is to store XML fragment instead of XML document. This is the best for logging-like applications, when you need to append new element to the end of file with no parsing of existing part. See http://www.tkachenko.com/blog/archives/000053.html
    Wednesday, November 09, 2005 9:45 AM
  • funny, I was reading your site from google! hopefully I can follow it...

    but how do I read the entire xml document in memory? then append the new object/element/data?
    Wednesday, November 09, 2005 3:16 PM
  • u can store it in the form of vector and store it even doc using stream but i need the data which is previous;y stored in the xml... is any one having complete solution to this??

     

    Sunday, April 11, 2010 2:15 PM
  •  XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(path);
                    XmlElement newelem = xmldoc.CreateElement("you element name");
                    newelem.InnerText = " NOTIFICATION:" + e.FullPath + " was " + e.ChangeType;
                    xmldoc.DocumentElement.InsertAfter(newelem, xmldoc.DocumentElement.LastChild);
                    xmldoc.Save(path);

    • Proposed as answer by SyedSheheryar Thursday, February 03, 2011 7:03 PM
    Wednesday, February 02, 2011 10:30 PM