This Article shows how to merge two XML files and make use of a single XML inorder to make the process simpler. Here XMLDocument is used in order to transfer the nodes from one XML to the other. So first step is to open the XML using the XML Document as shown below
System.Xml.XmlDocument objFirstXML = new XmlDocument();
objFirstXML.Load("C:\\temp\\XML1.xml");
System.Xml.XmlDocument objSecondXML = new XmlDocument();objSecondXML.Load("C:\\temp\\XML2.xml");
System.Xml.XmlDocument objSecondXML = new XmlDocument();
objSecondXML.Load("C:\\temp\\XML2.xml");
XmlNode objInsert = objFirstXML.SelectSingleNode("/XMLROOT/CHILD"); foreach( XmlNode objNode in objInsert.SelectNodes("/XMLROOT/CHILD/CHILDITEM")) { objInsert.AppendChild(objSecondXML.ImportNode(objNode,true)); } objFirstXML.Save("C:\\temp\\Merged.xml");
XmlNode objInsert = objFirstXML.SelectSingleNode("/XMLROOT/CHILD");
foreach( XmlNode objNode in objInsert.SelectNodes("/XMLROOT/CHILD/CHILDITEM"))
{
objInsert.AppendChild(objSecondXML.ImportNode(objNode,true));
}
objFirstXML.Save("C:\\temp\\Merged.xml");
Merge 2 XML files into single XML using C#
XMLDocument (DOM) and XDocument (LINQ)