Jul 10, 2012

How To Merge 2 or more Xml files and create Master Xml File


Step1: first create a sample folder in c drive
Step2: create 3 sample XML files with name books1, books2,books3
Step3: the sample file are looks like

Books.xml:



<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
   </book>
   <book id="bk102">
      <author>Jeanette, Dasha</author>
      <title>Quack the Duck</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
   </book>
</catalog>


Books1.xml:



<?xml version="1.0"?>
<catalog>
<book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
   </book>
   <book id="bk107">
      <author>Vinzovskaia, Irina</author>
      <title>Piano Fort A</title>
      <genre>Romance</genre>
      <price>4.95</price>
   </book>
</catalog>



Books2.xml:



<?xml version="1.0"?>
<catalog>
<book id="bk111">
      <author>Satya</author>
      <title>dotnet</title>
      <genre>study</genre>
      <price>4.95</price>
   </book>
   <book id="bk122">
      <author>vamsi</author>
      <title>Mysql</title>
      <genre>Database</genre>
      <price>4.95</price>
   </book>
</catalog>


Step4: add button to the from and write the below code in double click event


private void btnxml_Click(object sender, EventArgs e)
        {
            try
            {

XmlTextReader xmlreader1 = new XmlTextReader("C:\\test\\Books.xml");
 XmlTextReader xmlreader2 = new XmlTextReader("C:\\test\\Books2.xml");
 XmlTextReader xmlreader3 = new XmlTextReader("C:\\test\\Books1.xml");

                DataSet ds = new DataSet();
                ds.ReadXml(xmlreader1);
                DataSet ds2 = new DataSet();
                ds2.ReadXml(xmlreader2);
                ds.Merge(ds2);
                DataSet ds3 = new DataSet();
                ds3.ReadXml(xmlreader3);
                ds.Merge(ds3);
                ds.WriteXml("C:\\test\\MasterBooks.xml");
                MessageBox.Show("Completed merging XML documents");


 }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }




Step5: finally Master file will be generated and it looks like

Masterbook.xml:





<?xml version="1.0" standalone="yes"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
  </book>
  <book id="bk102">
    <author>Jeanette, Dasha</author>
    <title>Quack the Duck</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
  </book>
  <book id="bk111">
    <author>Satya</author>
    <title>dotnet</title>
    <genre>study</genre>
    <price>4.95</price>
  </book>
  <book id="bk122">
    <author>vamsi</author>
    <title>Mysql</title>
    <genre>Database</genre>
    <price>4.95</price>
  </book>
  <book id="bk106">
    <author>Randall, Cynthia</author>
    <title>Lover Birds</title>
    <genre>Romance</genre>
    <price>4.95</price>
  </book>
  <book id="bk107">
    <author>Vinzovskaia, Irina</author>
    <title>Piano Fort A</title>
    <genre>Romance</genre>
    <price>4.95</price>
  </book>
</catalog>







No comments:

Post a Comment