HOW TO: BETA: Merge Data from Two XML Documents by Using System.Xml with Visual Basic .NET (308066)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft XML Classes (included with the .NET Framework 1.0)
  • Microsoft XML Classes (included with the .NET Framework 1.1)

This article was previously published under Q308066
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.

For a Microsoft Visual C# .NET version of this article, see 311530.

IN THIS TASK

SUMMARY

This article demonstrates 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.

back to the top

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 Basic .NET syntax
  • Extensible Markup Language (XML)
back to the top

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>Ralls, Kim</author>
          <title>Midnight Rain</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.
back to the top

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>Thurman, Paula</author>
          <title>Splish Splash</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.
back to the top

Steps to Create the Visual Basic .NET Application

  1. Start Visual Studio .NET, and create a new Visual Basic Console Application project.
  2. Add the following code to the top of the Code window:
    Imports System.Xml
    					
  3. Copy and paste the following code in the Sub Main procedure:
            Dim xmlreader1 As New XmlTextReader("C:\Books1.xml")
    
            Dim xmlreader2 As New XmlTextReader("C:\Books2.xml")
    
            Dim ds1 As New DataSet()
            Try
                ds1.ReadXml(xmlreader1)
    
                Dim ds2 As New DataSet()
                ds2.ReadXml(xmlreader2)
    
                ds1.Merge(ds2)
                ds1.WriteXml("C:\Books.xml", XmlWriteMode.IgnoreSchema)
                Console.WriteLine("Completed merging XML documents")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            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 specified path.
  6. Open Books.xml. Notice that the data from Books2.xml is appended to the end to Books1.xml.
back to the top

Different XML Document Scenarios

Merge XML Documents That Have the Same Structure

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

back to the top

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>Bill Gates</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    <Customer>
              <CustomerName>Bill Gates</CustomerName>
              <PricePerUnit>5.95</PricePerUnit>
              <ClosingDate>2001-12-16</ClosingDate>
    </Customer>
    </SampleData>
    					
  2. Save Books2.xml.
  3. Run the Visual Basic .NET project again. Notice that the nodes from the second document (Books2.xml) are appended to the first XML document (Books1.xml).
back to the top

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>Thurman, Paula</author>
          <title>Splish Splash</title>
          <genre>Romance</genre>
          <price>4.95</price>
          <publish_date>2000-11-02</publish_date>
          <description>A deep-sea diver finds true love twenty 
          thousand leagues beneath the sea.</description>
       </book>
    </catalog>
    					
  2. Save Books2.xml.
  3. Run the Visual Basic .NET project again. Notice that the nodes from the second document are appended to the first XML document.
back to the top

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>Thurman, Paula</author>
          <title>Splish Splash</title>
          <price>4.95</price>
       </book>
       <book id="bk108" genre="Horror">
          <author>Knorr, Stefan</author>
          <title>Creepy Crawlies</title>
          <price>4.95</price>
       </book>
       </catalog>
    					
  2. Save Books2.xml.
  3. Run the Visual Basic .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.
back to the top

Merge XML Documents with Similar Structure Where First Document Contains Attributes

  1. Modify the Visual Basic code as follows so that Books1.xml is appended to Books2.xml:
            Dim xmlreader1 As New XmlTextReader("C:\Books2.xml")
    
            Dim xmlreader2 As New XmlTextReader("C:\Books1.xml")
    
            Dim ds1 As New DataSet()
            Try
                ds1.ReadXml(xmlreader1)
    
                Dim ds2 As New DataSet()
                ds2.ReadXml(xmlreader2)
    
                ds1.Merge(ds2)
                ds1.WriteXml("C:\Books.xml", XmlWriteMode.IgnoreSchema)
                Console.WriteLine("Completed merging XML documents")
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.Read()
    					
  2. Run the Visual Basic .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.
back to the top

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

286817 HOWTO: Merge Data in Two XML Documents by Programming the Document Object Model (DOM)

back to the top

Modification Type:MajorLast Reviewed:9/4/2003
Keywords:kbHOWTOmaster KB308066 kbAudDeveloper