How to merge data from two XML documents by using System.Xml with Visual C++ .NET or with Visual C++ 2005 (815678)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft XML Classes (included with the .NET Framework 1.1)
  • Microsoft XML Classes (included with the .NET Framework 1.0)

SUMMARY

This step-by-step article describes how to use a DataSet object to merge two XML documents. The DataSet object is central to supporting disconnected, distributed data scenarios with Microsoft ADO.NET. The DataSet object is a memory-resident representation of data that provides a consistent, relational programming model regardless of the data source. The DataSet object 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 2000 , Microsoft Windows XP, or Microsoft Windows Server 2003
  • Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Microsoft Visual C++ .NET syntax or Microsoft Visual C++ 2005
  • XML
back to the top

Create the Books1.xml file

  1. Paste the following code in Notepad, or a text editor of your choice:
    <?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>
    
  2. On the File menu, click Save As.
  3. In the Save As dialog box, click All Files in the Save As Type list.
  4. Click Unicode in the Encoding list, type Books1.xml in the File Name text box, and then click Save.
back to the top

Create the Books2.xml file

  1. Paste the following code in a new instance of Notepad:
    <?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>
    
  2. On the File menu, click Save As.
  3. In the Save As dialog box, click All Files in the Save As Type list, and then click Unicode in the Encoding list.
  4. In the File Name text box, type Books2.xml, and then click Save.
back to the top

Create the Visual C++ .NET application

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. In Visual Studio .NET 2002, create a new Managed C++ Application project, and then name it Q815678.

    In Visual Studio .NET 2003, create a new Visual C++ Console Application (.NET) project, and then name it Q815678.

    In Visual Studio 2005, create a new Visual C++ Console Application project, and then name it Q815678.
  3. Add the following code to the Q815678.cpp file before #include "stdafx.h":
    #using <System.xml.dll>
    using namespace System;
    using namespace System::Xml;
    using namespace System::IO;
    using namespace System::Data ;
    
  4. Paste the following code in the _tmain() function:
    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();
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  5. Build and run the application. Notice that you receive the following message in the Console window:Completed merging XML documents
  6. Close the Console window. Notice that the Books.xml file is created in the path that you specify.
  7. Open Books.xml. Notice that the data from Books2.xml is appended to the end of Books1.xml.
back to the top

Different XML document scenarios

Merge XML documents that have the same structure

The Visual C++ .NET samples in the three earlier sections of this article demonstrate 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 then 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 metro gnomes at a key base.</description>
    	   </book>
    	</catalog>
    
  2. Save Books2.xml.
  3. Run the Visual C++ .NET or Visual C++ 2005 project again. Open Books.xml. Notice that the nodes from the second document are appended to the first XML document.
back to the top

Merge XML documents with similar structures where the 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 metro gnomes at a key base.</description>
    	   </book>
    	</catalog>
    
  2. Save Books2.xml.
  3. Run the Visual C++ .NET or Visual C++ 2005 project again. Open Books.xml. Notice that the nodes from the second document are appended to the first XML document.

Merge XML documents with similar structures where the 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 or Visual C++ 2005 project again. Open Books.xml. Notice that the nodes from the second document are appended to the first XML document, and that the structure is same as the first XML document.
back to the top

Merge XML documents with similar structures where the 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 or Visual C++ 2005 project again. Open Books.xml. Notice that the resultant XML document appends the nodes from Books1.xml to Books2.xml. Additionally, notice that all 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 following article number 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:1/11/2006
Keywords:kbcode kbHOWTOmaster KB815678 kbAudDeveloper