Copy XmlNode from one XmlDocument object to another XmlDocument object

int NId=10;
// Create and load source XmlDocument object
XmlDocument SrcXmlDoc = new XmlDocument();
SrcXmlDoc.LoadXml(SrcXMLString);
//get xml node from source xml document
XmlNodeList SrcNodeList = SrcXmlDoc.SelectNodes(“/AddFlow/Node[./Url='” + NId + “‘]”);
// Create and load source XmlDocument object
XmlDocument TrgXmlDoc = new XmlDocument();
TrgXmlDoc.LoadXml(TrgXMLString);
// get xml node from target xml document
XmlNodeList TrgNodeList = TrgXmlDoc.SelectNodes(“/AddFlow/Node[./Url='” + NId + “‘]”);
// check xml node already exists or not
if (TrgNodeList != null & TrgNodeList.Count>0)
{
// remove xml node TrgXmlDoc.SelectNodes(“/AddFlow”).Item(0).RemoveChild(TrgNodeList.Item(0));
}
// import source xml node using SelectSingleNode and ImportNode methods
XmlNode SrcImpNode=SrcXmlDoc.SelectSingleNode(“/AddFlow/Node[./Url='” + NId + “‘]”);
XmlNode NewNode= TrgXmlDoc.ImportNode(SrcImpNode, true);
// Insert node into target xml document TrgXmlDoc.SelectNodes(“/AddFlow”).Item(0).AppendChild(NewNode);

Leave a Reply