Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Append new node to XML file

Append new node to XML file

Scheduled Pinned Locked Moved C#
questionxmltutoriallearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saud_a_k
    wrote on last edited by
    #1

    I have written an XML file using this code

    private void AddBook(string ISBN, string title, string author)
    {

    //
    // Initialize booksElement if you are adding your first book.
    //
    if (booksElement == null)
    {

    booksElement = xmlDoc.CreateElement("Books");

    }

    //
    // Same as tutorial; Adding and populating the elements
    //
    XmlElement bookElement = xmlDoc.CreateElement("Book");

    XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
    bookElement.SetAttributeNode(bookAttribute);
    bookAttribute.Value = ISBN;

    XmlElement titleElement = xmlDoc.CreateElement("title");
    titleElement.InnerText = title;
    bookElement.AppendChild(titleElement);

    XmlElement authorElement = xmlDoc.CreateElement("author");
    authorElement.InnerText = author;
    bookElement.AppendChild(authorElement);

    booksElement.AppendChild(bookElement);

    xmlDoc.AppendChild(booksElement);
    xmlDoc.Save(path);

    }

    it works fine for a new book, but I cannot OPEN an existing xml file and add a book (node) to it. How do I append a node??

    _____________________________________________________ Yea! I could be wrong...

    M 1 Reply Last reply
    0
    • S saud_a_k

      I have written an XML file using this code

      private void AddBook(string ISBN, string title, string author)
      {

      //
      // Initialize booksElement if you are adding your first book.
      //
      if (booksElement == null)
      {

      booksElement = xmlDoc.CreateElement("Books");

      }

      //
      // Same as tutorial; Adding and populating the elements
      //
      XmlElement bookElement = xmlDoc.CreateElement("Book");

      XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
      bookElement.SetAttributeNode(bookAttribute);
      bookAttribute.Value = ISBN;

      XmlElement titleElement = xmlDoc.CreateElement("title");
      titleElement.InnerText = title;
      bookElement.AppendChild(titleElement);

      XmlElement authorElement = xmlDoc.CreateElement("author");
      authorElement.InnerText = author;
      bookElement.AppendChild(authorElement);

      booksElement.AppendChild(bookElement);

      xmlDoc.AppendChild(booksElement);
      xmlDoc.Save(path);

      }

      it works fine for a new book, but I cannot OPEN an existing xml file and add a book (node) to it. How do I append a node??

      _____________________________________________________ Yea! I could be wrong...

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Hi! You didn't show how you create the xmlDoc object, but XmlDocument has a Load() method you can use to open an existing xml file. If you do open an existing file, you don't have to create the root entry (I guess it's booksElement, but can use xmlDoc.SelectSingleNode("/Books") to retrieve a reference to the existing node.

      Regards, mav -- Black holes are the places where God divided by 0...

      S 1 Reply Last reply
      0
      • M mav northwind

        Hi! You didn't show how you create the xmlDoc object, but XmlDocument has a Load() method you can use to open an existing xml file. If you do open an existing file, you don't have to create the root entry (I guess it's booksElement, but can use xmlDoc.SelectSingleNode("/Books") to retrieve a reference to the existing node.

        Regards, mav -- Black holes are the places where God divided by 0...

        S Offline
        S Offline
        saud_a_k
        wrote on last edited by
        #3

        I'm sorry I posted only the function of creating one node... I grabbed code from www.kirupa.com , all of it is here..

        private XmlDocument xmlDoc = new XmlDocument();
        private XmlElement booksElement;

        private void AddBook(string ISBN, string title, string author)
        {

        //
        // Initialize booksElement if you are adding your first book.
        //
        if (booksElement == null)
        {

        booksElement = xmlDoc.CreateElement("Books");

        }

        XmlElement bookElement = xmlDoc.CreateElement("Book");

        XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
        bookElement.SetAttributeNode(bookAttribute);
        bookAttribute.Value = ISBN;

        XmlElement titleElement = xmlDoc.CreateElement("title");
        titleElement.InnerText = title;
        bookElement.AppendChild(titleElement);

        XmlElement authorElement = xmlDoc.CreateElement("author");
        authorElement.InnerText = author;
        bookElement.AppendChild(authorElement);

        booksElement.AppendChild(bookElement);

        }

        private void WriteToDisk(string path)
        {

        // Add the booksElement to our root element and write to disk
        xmlDoc.AppendChild(booksElement);
        xmlDoc.Save(path);

        }

        static void Main(string[] args)
        {

        Program bookList = new Program();

        bookList.AddBook("0553212419", "Sherlock Holmes: Complete Novels and Stories, Vol 1", "Sir Arthur Conan Doyle");
        bookList.AddBook("0743273567", "The Great Gatsby", "F. Scott Fitzgerald");
        bookList.AddBook("0684826976", "Undaunted Courage", "Stephen E. Ambrose");
        bookList.AddBook("0743203178", "Nothing Like It In the World", "Stephen E. Ambrose");

        bookList.WriteToDisk(@"C:\books.xml");

        }

        So you see this creates a file OK.... But If I cannot ue this function to Append to the saved books.xml file. meanwhile, I try the SelectSingleNode, and see if it works.

        _____________________________________________________ Yea! I could be wrong...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups