locked
Error when appending XML node RRS feed

  • Question

  • User-1682007629 posted

    I need to read an XML document, and then add nodes to it. In the example below I have a grandparent, and parent
    node. I then want to add sibling nodes to the document dynamically. I don't know what I am doing wrong.
    But at line 17 I get this error message: Error: This document already has a 'DocumentElement' node.

    Can someone help me figure out why this is not working?

     

    Imports System
    Imports System.Text
    Imports System.Xml
    Imports System.Xml.XPath
    Imports System.Xml.Schema
    Partial Class _Default
        Inherits System.Web.UI.Page


        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim TemplateDoc As New XmlDocument()
            TemplateDoc.Load("C:\\DeleteThis\\XMLFile.xml")
            Dim sib As XmlNode = TemplateDoc.SelectSingleNode("/GrandParent/Parent")
            Dim xmlSib As XmlNode = TemplateDoc.CreateNode(XmlNodeType.Element, "Sibling", Nothing)
            xmlSib.InnerXml = "Andy"
            sib.AppendChild(xmlSib)
            TemplateDoc.AppendChild(xmlSib) ' Error: This document already has a 'DocumentElement' node.

            xmlSib.InnerXml = "Vinny"
            sib.AppendChild(xmlSib)
            TemplateDoc.AppendChild(xmlSib)

        End Sub
    End Class

    Here is the XML I read in from the location C:\DeleteThis

    <?xml version="1.0" encoding="utf-8" ?>
    <GrandParent>
      <Parent>
      </Parent>
    </GrandParent>

    Wednesday, July 28, 2010 1:52 AM

Answers

  • User-1315824907 posted

    Here the proble is that you can create a document element in the 14th line..
    So this problem is comming

    Try This Code Hope this works 

     

    Imports System
    Imports System.Text
    Imports System.Xml
    Imports System.Xml.XPath
    Imports System.Xml.Schema
    Partial Class _Default
        Inherits System.Web.UI.Page


        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim TemplateDoc As New XmlDocument()
            TemplateDoc.Load("C:\\DeleteThis\\XMLFile.xml")
            Dim sib As XmlNode = TemplateDoc.SelectSingleNode("/GrandParent/Parent")
            Dim xmlSib As XmlNode = TemplateDoc.CreateNode(XmlNodeType.Element, "Sibling", Nothing)
            xmlSib.InnerXml = "Andy"
            sib.AppendChild(xmlSib)
            xmlSib.InnerXml = "Vinny"
            sib.AppendChild(xmlSib)
            TemplateDoc.AppendChild(xmlSib)

        End Sub
    End Class

    Here is the XML I read in from the location C:\DeleteThis

    <?xml version="1.0" encoding="utf-8" ?>
    <GrandParent>
      <Parent>
      </Parent>
    </GrandParent>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 28, 2010 2:11 AM
  • User-1293656743 posted

    Problem is on creating a node  document element is also get created.

    Have a look on below code to add node in Xml document.

     Dim TemplateDoc As New XmlDocument()
                TemplateDoc.Load("C:\\N.xml")
                Dim sib As XmlNode = TemplateDoc.SelectSingleNode("/GrandParent/Parent")
                Dim xmlSib As XmlNode = TemplateDoc.CreateNode(XmlNodeType.Element, "Sibling", Nothing)
                xmlSib.InnerXml = "Dinesh"
                sib.AppendChild(xmlSib)
                TemplateDoc.Save("C:\\N.xml")

                Dim TemplateDoc As New XmlDocument()

                TemplateDoc.Load("C:\\DeleteThis\\XMLFile.xmll")

                Dim sib As XmlNode = TemplateDoc.SelectSingleNode("/GrandParent/Parent")

                Dim xmlSib As XmlNode = TemplateDoc.CreateNode(XmlNodeType.Element, "Sibling", Nothing)

                xmlSib.InnerXml = "Andy"

                sib.AppendChild(xmlSib)

                TemplateDoc.Save("C:\\DeleteThis\\XMLFile.xml")


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 28, 2010 4:53 AM

All replies

  • User-1315824907 posted

    Here the proble is that you can create a document element in the 14th line..
    So this problem is comming

    Try This Code Hope this works 

     

    Imports System
    Imports System.Text
    Imports System.Xml
    Imports System.Xml.XPath
    Imports System.Xml.Schema
    Partial Class _Default
        Inherits System.Web.UI.Page


        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim TemplateDoc As New XmlDocument()
            TemplateDoc.Load("C:\\DeleteThis\\XMLFile.xml")
            Dim sib As XmlNode = TemplateDoc.SelectSingleNode("/GrandParent/Parent")
            Dim xmlSib As XmlNode = TemplateDoc.CreateNode(XmlNodeType.Element, "Sibling", Nothing)
            xmlSib.InnerXml = "Andy"
            sib.AppendChild(xmlSib)
            xmlSib.InnerXml = "Vinny"
            sib.AppendChild(xmlSib)
            TemplateDoc.AppendChild(xmlSib)

        End Sub
    End Class

    Here is the XML I read in from the location C:\DeleteThis

    <?xml version="1.0" encoding="utf-8" ?>
    <GrandParent>
      <Parent>
      </Parent>
    </GrandParent>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 28, 2010 2:11 AM
  • User-1293656743 posted

    Problem is on creating a node  document element is also get created.

    Have a look on below code to add node in Xml document.

     Dim TemplateDoc As New XmlDocument()
                TemplateDoc.Load("C:\\N.xml")
                Dim sib As XmlNode = TemplateDoc.SelectSingleNode("/GrandParent/Parent")
                Dim xmlSib As XmlNode = TemplateDoc.CreateNode(XmlNodeType.Element, "Sibling", Nothing)
                xmlSib.InnerXml = "Dinesh"
                sib.AppendChild(xmlSib)
                TemplateDoc.Save("C:\\N.xml")

                Dim TemplateDoc As New XmlDocument()

                TemplateDoc.Load("C:\\DeleteThis\\XMLFile.xmll")

                Dim sib As XmlNode = TemplateDoc.SelectSingleNode("/GrandParent/Parent")

                Dim xmlSib As XmlNode = TemplateDoc.CreateNode(XmlNodeType.Element, "Sibling", Nothing)

                xmlSib.InnerXml = "Andy"

                sib.AppendChild(xmlSib)

                TemplateDoc.Save("C:\\DeleteThis\\XMLFile.xml")


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 28, 2010 4:53 AM