darma teja

darma teja

  • NA
  • 496
  • 336.9k

merge all XML file in a folder in C#

Aug 18 2016 5:41 AM
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:
 
  1. <first>  
  2.    <second>  
  3.       <third>123</third>  
  4.          <text>  
  5.             <type>text1</type>  
  6.             <country>fr</country>  
  7.             <vaue>No1</value>  
  8.          </text>  
  9.       <third>345</third>  
  10.            <text>  
  11.            <type>text2</type>  
  12.            <country>fr</country>  
  13.            <vaue>No2</value>  
  14.          </text>  
  15.    </second>  
  16. </first>  
 My second xml file is like this:
  1. <first>  
  2.    <second>  
  3.       <third>123</third>  
  4.          <text>  
  5.             <type>text1</type>  
  6.             <country>in</country>  
  7.             <vaue>No5</value>  
  8.          </text>  
  9.       <third>345</third>  
  10.          <text>  
  11.             <type>text2</type>  
  12.             <country>in</country>  
  13.             <vaue>No3</value>  
  14.          </text>  
  15.       </second>  
  16. </first>  
The output should be like this:
  1. <first>  
  2.      <second>  
  3.          <third>123</third>  
  4.             <text>  
  5.                <type>text1</type>  
  6.                <country>fr</country>  
  7.                <vaue>No1</value>  
  8.             </text>  
  9.             <text>  
  10.                <type>text1</type>  
  11.                <country>in</country>  
  12.                <vaue>No5</value>  
  13.             </text>  
  14.          <third>345</third>  
  15.             <text>  
  16.                <type>text2</type>  
  17.                <country>in</country>  
  18.                <vaue>No3</value>  
  19.             </text>  
  20.             <text>  
  21.             <type>text2</type>  
  22.             <country>in</country>  
  23.             <vaue>No2</value>  
  24.          </text>  
  25.    </second>  
  26. </first>  
I have tried it in C# like this, but it is not giving me the correct output:
  1. string[] files = get all files in a directory;  
  2.   
  3. var masterfile = new XDocument();  
  4. string readAllText1 = File.ReadAllText(files[0]);  
  5. StreamReader sr = new StreamReader(files[0], true);  
  6. XDocument xdoc1 = XDocument.Load(sr);  
  7.   
  8. XElement newDocument = new XElement(xdoc1.Root);  
  9.   
  10. masterfile.Add(newDocument);  
  11. foreach (var file in files)  
  12. {  
  13. if (file != files[0])  
  14. {  
  15. StreamReader sr1 = new StreamReader(file, true);  
  16. XDocument xdoc = XDocument.Load(sr1);  
  17. masterfile.Root.Add(xdoc.Descendants("third"));  
  18. }  
  19. }  
  20. string str = Path.GetFileNameWithoutExtension(files[0]).Remove(Path.GetFileNameWithoutExtension(files[0]).Length - 3);  
  21. masterfile.Save(textBox2.Text + "//" + str + ".xml");  
Advance thanks,
Darma 

Answers (6)