Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append an xml document to an xml node in C#?

Tags:

c#

xml

How can I append an XML document to an xml node in c#?

like image 306
kjv Avatar asked Mar 11 '09 17:03

kjv


3 Answers

Yes:

XmlNode imported = targetNode.OwnerDocument.ImportNode(otherDocument.DocumentElement, true);
targetNode.AppendChild(imported);

I think this creates a clone of your document though.

like image 81
Grzenio Avatar answered Nov 15 '22 11:11

Grzenio


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);
like image 15
Cerebrus Avatar answered Nov 15 '22 11:11

Cerebrus


Perhaps like this:

XmlNode node = ......  // belongs to targetDoc (XmlDocument)

node.AppendChild(targetDoc.ImportNode(xmlDoc.DocumentElement));

Marc

like image 1
marc_s Avatar answered Nov 15 '22 12:11

marc_s