How To Merge Data from Two XML Documents Using System.Xml by Using Visual C# .NET

Article translations Article translations
Article ID: 311530 - View products that this article applies to.
This article was previously published under Q311530
Expand all | Collapse all

On This Page

Summary

This step-by-step article shows you how to use a DataSet object to merge two XML documents. The DataSet object is central to supporting disconnected, distributed data scenarios with ADO.NET. The DataSet is a memory-resident representation of data that provides a consistent, relational programming model regardless of the data source. The DataSet represents a complete set of data, including related tables, constraints, and relationships among the tables.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows XP, Windows 2000, or Windows NT 4.0 Service Pack 6a (SP6a)
  • Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Visual C# .NET syntax
  • Extensible Markup Language (XML)

Create the Books1.xml File

  1. From the Windows Start menu, click Run, type Notepad.exe, and then click OK.
  2. Highlight the following code, right-click the code, and then click Copy. In Notepad, on the Edit menu, click Paste.
    <?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>
    					
  3. On the File menu, click Save.
  4. In the Save As dialog box, in the Save As Type drop-down list box, click All Files. In the File Name text box, type Books1.xml, and then click Save.

Create the Books2.xml File

  1. In Notepad, create a new text file named Books2.xml.
  2. Copy and paste the following XML into Books2.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>
    					
  3. On the File menu, click Save.
  4. In the Save As dialog box, in the Save As Type drop-down list box, click All Files. In the File Name text box, type Books2.xml, and then click Save.

Steps to Create the Visual C# .NET Application

  1. Start Visual Studio .NET, and then create a new Visual C# Console Application project.
  2. Add the following code to the top of the Code window:
    using System;
    using System.Xml;
    using System.IO;
    using System.Data ;
    					
  3. Copy and paste the following code in static void Main(string[] args):
    try
    	{
    		XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books1.xml");
    		XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books2.xml");
    
    		DataSet ds = new DataSet();
    		ds.ReadXml(xmlreader1);
    		DataSet ds2 = new DataSet();
    		ds2.ReadXml(xmlreader2);
    		ds.Merge(ds2);
    		ds.WriteXml("C:\\Books.xml");
    		Console.WriteLine("Completed merging XML documents");
    	}
    	catch (System.Exception ex)
    	{
    		Console.Write(ex.Message);
    	}
    Console.Read();	
    					
  4. Build and run the application. Notice that the "Completed merging XML documents" message appears in the Console window.
  5. Close the Console window. Notice that the Books.xml file is created in the path that you specify.
  6. Open Books.xml. Notice that the data from Books2.xml is appended to the end to Books1.xml.

Different XML Document Scenarios

Merge XML Documents That Have the Same Structure

The preceding Visual C# .NET sample demonstrates the output of XML documents with the same structure.

Merge XML Documents That Have Different Structures

  1. Open Books2.xml, and replace the XML with the following XML:
    <?xml version="1.0" ?>
    <SampleData>
    <Customer>
              <CustomerName>Bob Frapples</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    <Customer>
              <CustomerName>Bob Frapples</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    </SampleData>
    					
  2. Save Books2.xml.
  3. Run the Visual C# .NET project again. Notice that the nodes from the second document (Books2.xml) are appended to the first XML document (Books1.xml).

Merge XML Documents with Similar Structure Where Second Document Contains Additional Elements

  1. Open Books2.xml, and replace the XML with the following XML:
    <?xml version="1.0"?>
    <catalog>
    <book id="bk106">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-09-02</publish_date>
          <description>When Carla meets Paul at an ornithology 
          conference, tempers fly as feathers get ruffled.</description>
       </book>
       <book id="bk107">
          <author>Vinzovskaia, Irina</author>
          <title>Piano Fort A</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-11-02</publish_date>
          <description>Two young pianists strike a chord when they fight a 
          pitched battle to stop the gnomes at a key base.</description>
       </book>
    </catalog>
    					
  2. Save Books2.xml.
  3. Run the Visual C# .NET project again. Notice that the nodes from the second document are appended to the first XML document.

Merge XML Documents with Similar Structure Where Second Document Contains Attributes

  1. Open Books2.xml, and replace the XML with the following XML:
    <?xml version="1.0"?>
    <catalog>
    <book id="bk106" genre="Romance">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <price>4.95</price>
       </book>
       <book id="bk107" genre="Romance">
          <author>Vinzovskaia, Irina</author>
          <title>Piano Fort A</title>
          <price>4.95</price>
       </book>
       <book id="bk108" genre="Horror">
          <author>de Dogg, Jazz</author>
          <title>Night of the Flea</title>
          <price>4.95</price>
       </book>
       </catalog>
    					
  2. Save Books2.xml.
  3. Run the Visual C# .NET project again. Notice that the nodes from the second document are appended to the first XML document, and the structure is same as the first XML document.

Merge XML Documents with Similar Structure Where First Document Contains Attributes

  1. Modify the Visual C# code as follows so that Books1.xml is appended to Books2.xml:
    try
    	{
    		XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books2.xml");
    		XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books1.xml");
    
    		DataSet ds = new DataSet();
    		ds.ReadXml(xmlreader1);
    		DataSet ds2 = new DataSet();
    		ds2.ReadXml(xmlreader2);
    		ds.Merge(ds2);
    		ds.WriteXml("C:\\Books.xml");
    		Console.WriteLine("Completed merging XML documents");
    	}
    	catch (System.Exception ex)
    	{
    		Console.Write(ex.Message);
    	}
    Console.Read();	
    					
  2. Run the Visual C# .NET project again. Notice that the resultant XML document appends the nodes from Books1.xml to Books2.xml. In addition, notice that all of the Book nodes contain the "genre" attribute.

    Therefore, depending on the structure of the first XML document, the second XML document is modified so that the resultant XML is more meaningful.

References

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
286817 How To Merge Data in Two XML Documents by Programming the Document Object Model (DOM)

Properties

Article ID: 311530 - Last Review: August 30, 2012 - Revision: 3.0
Applies to
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
Keywords: 
kbhowtomaster kbio kbsystemdata KB311530

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com