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. adding nodes to a xml document in c#

adding nodes to a xml document in c#

Scheduled Pinned Locked Moved C#
helpcsharpbusinessxmltutorial
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.
  • L Offline
    L Offline
    laziale
    wrote on last edited by
    #1

    hello everyone out there! I have one problem and I hope someone can help me. There is one project what I am working on and is mostly data scraping and saving data in xml document. I have an option to create lists and then add data to the lists. For example, the user can create list for restaurants, and I am creating xml document just with one xml element: . Then once I have all the data scraped I want to add xml nodes with the data what I have to the existing document. I am trying something like this:

    string path = Properties.Settings.Default.XMLFolder + list + ".xml";
    XmlTextReader rdr = new XmlTextReader(path);
    XmlDocument doc = new XmlDocument();
    doc.Load(rdr);
    XmlNode nodeBusiness = doc.CreateNode(XmlNodeType.Element, "Business", string.Empty);
    nodeBusiness.Attributes.Append(attribute);
    docData.AppendChild(nodeBusiness);

    I am getting an error at the last step: "This document already has a 'DocumentElement' node." Can someone help me to solve this issue, thanks in advance. Laziale

    L 1 Reply Last reply
    0
    • L laziale

      hello everyone out there! I have one problem and I hope someone can help me. There is one project what I am working on and is mostly data scraping and saving data in xml document. I have an option to create lists and then add data to the lists. For example, the user can create list for restaurants, and I am creating xml document just with one xml element: . Then once I have all the data scraped I want to add xml nodes with the data what I have to the existing document. I am trying something like this:

      string path = Properties.Settings.Default.XMLFolder + list + ".xml";
      XmlTextReader rdr = new XmlTextReader(path);
      XmlDocument doc = new XmlDocument();
      doc.Load(rdr);
      XmlNode nodeBusiness = doc.CreateNode(XmlNodeType.Element, "Business", string.Empty);
      nodeBusiness.Attributes.Append(attribute);
      docData.AppendChild(nodeBusiness);

      I am getting an error at the last step: "This document already has a 'DocumentElement' node." Can someone help me to solve this issue, thanks in advance. Laziale

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      change it from : docData.AppendChild(nodeBusiness); to: docData.DocumentElement.AppendChild(nodeBusiness);

      L 1 Reply Last reply
      0
      • L Lost User

        change it from : docData.AppendChild(nodeBusiness); to: docData.DocumentElement.AppendChild(nodeBusiness);

        L Offline
        L Offline
        laziale
        wrote on last edited by
        #3

        Thanks a lot, that solved my problem. Have a nice day, Laziale

        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