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 TpPosted Aug 18, 2016, 7:29 AM
darma tejaPosted Aug 18, 2016, 7:38 AM
darma tejaPosted Aug 18, 2016, 7:05 AM
Midhun TpPosted Aug 18, 2016, 6:54 AM
darma tejaPosted Aug 18, 2016, 6:30 AM
Vinay SinghPosted Aug 18, 2016, 6:15 AM