SIGN UP MEMBER LOGIN:    
ARTICLE

Merge 2 XML files into single XML using C#

Posted by Karthikeyan Anbarasan Articles | XML in C# March 17, 2011
This article gives a basic idea on how to merge 2 XML files into a single XML using C#.
Reader Level:

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");
Similar way we open the second XML using the XMLDocument.
 
System.Xml.XmlDocument objSecondXML = new XmlDocument();
objSecondXML.Load("C:\\temp\\XML2.xml"); 
 
Here once we open the xml now search for the node and then append it to the main one
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"); 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor