|
|
|
The General Winforms Interview Questions consists the most frequently
asked questions in Winforms. This list of 100+ questions guage your familiarity
with the Winforms platform. The q&a have been collected over a period
of time from various blogs, forums and other similar Winforms sites
|
57.Framework Tips XML
|
| 57.1 How do I add an XmlNode from one XmlDocument to an XmlNode in another XmlDocument?
|
57.1 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
|