I want to merge all xml files in a folder. Here i am giving sample two xml files:
My first xml file is like this:
- <first>
- <second>
- <third>123third>
- <text>
- <type>text1type>
- <country>frcountry>
- <vaue>No1value>
- text>
- <third>345third>
- <text>
- <type>text2type>
- <country>frcountry>
- <vaue>No2value>
- text>
- second>
- first>
- <first>
- <second>
- <third>123third>
- <text>
- <type>text1type>
- <country>incountry>
- <vaue>No5value>
- text>
- <third>345third>
- <text>
- <type>text2type>
- <country>incountry>
- <vaue>No3value>
- text>
- second>
- first>
The output should be like this:
- <first>
- <second>
- <third>123third>
- <text>
- <type>text1type>
- <country>frcountry>
- <vaue>No1value>
- text>
- <text>
- <type>text1type>
- <country>incountry>
- <vaue>No5value>
- text>
- <third>345third>
- <text>
- <type>text2type>
- <country>incountry>
- <vaue>No3value>
- text>
- <text>
- <type>text2type>
- <country>incountry>
- <vaue>No2value>
- text>
- second>
- first>
I have tried it in C# like this, but it is not giving me the correct output:
- string[] files = get all files in a directory;
- var masterfile = new XDocument();
- string readAllText1 = File.ReadAllText(files[0]);
- StreamReader sr = new StreamReader(files[0], true);
- XDocument xdoc1 = XDocument.Load(sr);
- XElement newDocument = new XElement(xdoc1.Root);
- masterfile.Add(newDocument);
- foreach (var file in files)
- {
- if (file != files[0])
- {
- StreamReader sr1 = new StreamReader(file, true);
- XDocument xdoc = XDocument.Load(sr1);
- masterfile.Root.Add(xdoc.Descendants("third"));
- }
- }
- string str = Path.GetFileNameWithoutExtension(files[0]).Remove(Path.GetFileNameWithoutExtension(files[0]).Length - 3);
- masterfile.Save(textBox2.Text + "//" + str + ".xml");
Darma

Midhun TpRank #388 · 6.5K reputationPosted Aug 18, 2016, 3:29 AM EDT
Midhun TpRank #388 · 6.5K reputationPosted Aug 18, 2016, 2:54 AM EDT
Vinay SinghRank #604 · 4K reputationPosted Aug 18, 2016, 2:15 AM EDT
darma tejaRank #3,199 · 449 reputationPosted Aug 18, 2016, 2:30 AM EDT
darma tejaRank #3,199 · 449 reputationPosted Aug 18, 2016, 3:05 AM EDT
darma tejaRank #3,199 · 449 reputationPosted Aug 18, 2016, 3:38 AM EDT