insert XmlDocument into a XmlDocument node
Asked Answered
G

2

9

I created a basic XmlDocument with one node:

XmlDocument bigDoc = new XmlDocument();
bigDoc.LoadXml("<Request></Request>");

and I'm getting another XmlDocument that I want to insert inside <Request> node. It doesn't work for me:

 XmlNode requestNode =  bigDoc.FirstChild;
 requestNode.AppendChild(anotherXMLDocument);

It thorows an exception.

How can I insert a XmlDocument inside another XmlDocument node?

Gynecology answered 6/10, 2010 at 11:31 Comment(0)
V
17

If I recall correctly that it's basically the same thing in every DOM Implementation around (.net, javascript, php etc. this should work.

XmlNode requestNode =  bigDoc.FirstChild;
requestNode.AppendChild(
    requestNode.OwnerDocument.ImportNode(
        anotherXMLDocument.DocumentElement, true));

The true (2nd argument to importNode) should mean import deep.

Vostok answered 6/10, 2010 at 12:0 Comment(1)
Couldn't find that in XmlDocumentTomasatomasina
P
-1
Public Sub rutina(ByRef Sobre As String, ByVal Cfe As String)
    'Agrega CFE al final de sobre, que puede ya contener
    'otro(s) CFE

    'Abre el sobre.
    Dim doc As New XmlDocument()
    doc.Load(Sobre)

    'Abre el xml con el nuevo CFE
    Dim doc2 As New XmlDocument()
    doc2.Load(Cfe)

    'Importa el CFE al final del sobre (antes de </Fin> )
    Dim newBook As XmlNode = doc.ImportNode(doc2.DocumentElement, True)
    doc.DocumentElement.AppendChild(newBook)

    doc.Save(Sobre)
End sub
Paraffinic answered 16/2, 2017 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.