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>123</third>
- <text>
- <type>text1</type>
- <country>fr</country>
- <vaue>No1</value>
- </text>
- <third>345</third>
- <text>
- <type>text2</type>
- <country>fr</country>
- <vaue>No2</value>
- </text>
- </second>
- </first>
My second xml file is like this:
- <first>
- <second>
- <third>123</third>
- <text>
- <type>text1</type>
- <country>in</country>
- <vaue>No5</value>
- </text>
- <third>345</third>
- <text>
- <type>text2</type>
- <country>in</country>
- <vaue>No3</value>
- </text>
- </second>
- </first>
The output should be like this:
- <first>
- <second>
- <third>123</third>
- <text>
- <type>text1</type>
- <country>fr</country>
- <vaue>No1</value>
- </text>
- <text>
- <type>text1</type>
- <country>in</country>
- <vaue>No5</value>
- </text>
- <third>345</third>
- <text>
- <type>text2</type>
- <country>in</country>
- <vaue>No3</value>
- </text>
- <text>
- <type>text2</type>
- <country>in</country>
- <vaue>No2</value>
- </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");
Advance thanks,
Darma