teradyne
1/31/2012 - 6:31 PM

Merge multiple XML files into single file given a node.

Merge multiple XML files into single file given a node.



//Merge Multiple XML files into a single XML file with a specific node. 
//For quickest result use linqpad.
//Language: C# Statement(s)

var downloadfolder = @"G:\temp\"; // your download folder where there are multiple files 
string[] files = Directory.GetFiles(downloadfolder);
var masterfile = new XDocument(); 
XElement newDocument = new XElement("root");
masterfile.Add(newDocument);
foreach (var file in files)
{	
 	Console.WriteLine("--------------------------------------------------");
 	Console.WriteLine(file);
 	Console.WriteLine("--------------------------------------------------");
 	XDocument xdoc = XDocument.Load(file);
 	masterfile.Root.Add(xdoc.Descendants("company")); //your root note
}
masterfile.Dump();
masterfile.Save( @"G:\temp\merged_.xml");