How can I append an XML document to an xml node in c#?
+1
A:
Perhaps like this:
XmlNode node = ...... // belongs to targetDoc (XmlDocument)
node.AppendChild(targetDoc.ImportNode(xmlDoc.DocumentElement));
Marc
marc_s
2009-03-11 17:04:55
AFAIK, you are required to **import* a node if it doesn't belong to the current XmlDocument before you can append it. See my answer.
Cerebrus
2009-03-11 17:13:04
Yes, seems you need to call ImportNode indeed, but that *will* create a copy of the Xml document.....
marc_s
2009-03-11 17:48:27
+4
A:
Yes:
XmlNode imported = targetNode.OwnerDocument.ImportNode(otherDocument.DocumentElement, true);
targetNode.AppendChild(imported);
I think this creates a clone of your document though.
Grzenio
2009-03-11 17:06:37
A:
Once you have the root node of the XML document in question you can append it as a child node of the node in question. Does that make sense?
Gregory A Beamer
2009-03-11 17:06:48
+7
A:
An XmlDocument
is basically an XmlNode
, so you can append it just like you would do for any other XmlNode
. However, the difference arises from the fact that this XmlNode
does not belong to the target document, therefore you will need to use the ImportNode method and then perform the append.
// xImportDoc is the XmlDocument to be imported.
// xTargetNode is the XmlNode into which the import is to be done.
XmlNode xChildNode = xSrcNode.ImportNode(xImportDoc, true);
xTargetNode.AppendChild(xChildNode);
Cerebrus
2009-03-11 17:10:58