Skip to content

Instantly share code, notes, and snippets.

@teradyne
Created January 31, 2012 18:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teradyne/1712060 to your computer and use it in GitHub Desktop.
Save teradyne/1712060 to your computer and use it in GitHub Desktop.
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");
@Avi491
Copy link

Avi491 commented Oct 19, 2020

Hi friend,

I tried this but i am getting an error (Error: 'XDocument' does not contain a definition for 'Dump' and no extension method 'Dump' accepting a first argument of type 'XDocument'could be found ) on masterfile.Dump();
Can anyone help regarding this error ???
WhatsApp Image 2020-10-19 at 13 34 03

@GXelha
Copy link

GXelha commented Oct 29, 2020

Hi friend,

I tried this but i am getting an error (Error: 'XDocument' does not contain a definition for 'Dump' and no extension method 'Dump' accepting a first argument of type 'XDocument'could be found ) on masterfile.Dump();
Can anyone help regarding this error ???
WhatsApp Image 2020-10-19 at 13 34 03

This was created using LINQPad.
This .Dump() is an extension method provided there.
It can be discarded elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment