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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. XML / XSL
  4. problem updating a xml doc with AppendChild

problem updating a xml doc with AppendChild

Scheduled Pinned Locked Moved XML / XSL
helpxmljsonquestionannouncement
2 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.
  • D Offline
    D Offline
    dratcha
    wrote on last edited by
    #1

    I am trying to insert new nodes within an existing XML document. The insert that I am having problems with is a few nodes deep. I can insert a new node just below the root node with the following with no problems: XmlDocument doc = new XmlDocument(); doc.Load(strXMLPath); XmlNode tasks = doc.SelectSingleNode("/tasks"); doc.AppendChild(tasks); // The rest of the AddElement and AddAttribute stuff has been omitted. Now, here is the code which tries to write a little deeper down and is breaking: XmlDocument doc = new XmlDocument(); doc.Load(strXMLPath); XmlNode checks = doc.SelectSingleNode("/tasks/task[@id='1001']/checks"); doc.AppendChild(checks); // The rest of the AddElement and AddAttribute stuff has been omitted. The error the second code snippet gives is: This document already has a DocumentElement node. It gives this error on the doc.AppendChild(checks); line I just can't figure it out. I did a quick watch on XmlNode checks = doc.SelectSingleNode("/tasks/task[@id='1001']/checks"); and it did put me in the correct node. I have no idea why I can not append into this node. Anyone have any ideas? Here is my XML doc if you want to see it for reference: <?xml version="1.0" encoding="utf-8"?> <tasks> <task id="1000"> <name>Task Name</name> <checks> <check id="1000_1"> <time>14:00</time> </check> <check id="1000_2"> <time>23:00</time> </check> </checks> </task> </tasks>

    R 1 Reply Last reply
    0
    • D dratcha

      I am trying to insert new nodes within an existing XML document. The insert that I am having problems with is a few nodes deep. I can insert a new node just below the root node with the following with no problems: XmlDocument doc = new XmlDocument(); doc.Load(strXMLPath); XmlNode tasks = doc.SelectSingleNode("/tasks"); doc.AppendChild(tasks); // The rest of the AddElement and AddAttribute stuff has been omitted. Now, here is the code which tries to write a little deeper down and is breaking: XmlDocument doc = new XmlDocument(); doc.Load(strXMLPath); XmlNode checks = doc.SelectSingleNode("/tasks/task[@id='1001']/checks"); doc.AppendChild(checks); // The rest of the AddElement and AddAttribute stuff has been omitted. The error the second code snippet gives is: This document already has a DocumentElement node. It gives this error on the doc.AppendChild(checks); line I just can't figure it out. I did a quick watch on XmlNode checks = doc.SelectSingleNode("/tasks/task[@id='1001']/checks"); and it did put me in the correct node. I have no idea why I can not append into this node. Anyone have any ideas? Here is my XML doc if you want to see it for reference: <?xml version="1.0" encoding="utf-8"?> <tasks> <task id="1000"> <name>Task Name</name> <checks> <check id="1000_1"> <time>14:00</time> </check> <check id="1000_2"> <time>23:00</time> </check> </checks> </task> </tasks>

      R Offline
      R Offline
      rudy net
      wrote on last edited by
      #2

      Why would you want to append the same node that you queried? I would suggest the following: XmlNode checks = doc.SelectSingleNode("/tasks/task[@id='1001']/checks"); XmlNode newCheck,newNode; newCheck = doc.CreateElement("check"); XmlAttribute attr = doc.CreateAttribute("id"); attr.Value = "1000_3"; newCheck.Attributes.Append(attr); //add child node of new check newNode = doc.CreateElement("time"); newNode.Value = "15:00"; newCheck.AppendChild(newNode); checks.AppendChild(newCheck);

      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