How do I add an XmlNode from one XmlDocument to an XmlNode in another XmlDocument?
The XmlNode.AppendChild restricts you to add XmlNodes originated in the same XmlDocument. So, to do the above, here is a sample workaround:
[C#]
// destParent and sourceParent are from different XmlDocuments.
// This method will append the children of sourceParent to the destParent’s children.
public static void TransferChildren(XmlDocument destDoc, XmlNode destParent, XmlNode sourceParent)
{
// Create a temporary element into which we will add the children.
XmlElement tempElem = destDoc.CreateElement('Temp');
tempElem.InnerXml = sourceParent.InnerXml;
foreach(XmlNode node in tempElem.ChildNodes)
destParent.AppendChild(node);
}
[VB.Net]
’ destParent and sourceParent are from different XmlDocuments.
’ This method will append the children of sourceParent to the destParent’s children.
Public Static Sub TransferChildren(ByVal destDoc As XmlDocument, ByVal destParent As XmlNode, ByVal sourceParent As XmlNode)
’ Create a temporary element into which we will add the children.
Dim tempElem As XmlElement = destDoc.CreateElement('Temp')
tempElem.InnerXml = sourceParent.InnerXml
Dim node As XmlNode
For Each node In tempElem.ChildNodes
destParent.AppendChild(node)
Next
End Sub
Permalink
Share with