Microsoft KB Archive/311530

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

Article ID: 311530

Article Last Modified on 7/15/2004

-

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

-



This article was previously published under Q311530



For a Microsoft Visual Basic .NET version of this article, see 308066.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Xml
 * System.Io
 * System.Data

IN THIS TASK
SUMMARY Requirements Create the Books1.xml File Create the Books2.xml File Steps to Create Visual C# .NET Application Different XML Document Scenarios
 * Merge XML Documents That Have the Same Structure
 * Merge XML Documents That Have Different Structures
 * Merge XML Documents with Similar Structure Where Second XML Document Contains Additional Elements
 * Merge XML Documents with Similar Structure Where Second XML Document Contains Attributes
 * Merge XML Documents with Similar Structure Where First XML Document Contains Attributes

REFERENCES



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.

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 C# .NET syntax
 * Extensible Markup Language (XML)

back to the top

Create the Books1.xml File
 From the Windows Start menu, click Run, type Notepad.exe, and then click OK.  Highlight the following code, right-click the code, and then click Copy. In Notepad, on the Edit menu, click Paste.   Gambardella, Matthew XML Developer's Guide Computer 44.95    Jeanette, Dasha Quack the Duck Fantasy 5.95                     On the File menu, click Save. 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
 In Notepad, create a new text file named Books2.xml.</li>  Copy and paste the following XML into Books2.xml: <?xml version=&quot;1.0&quot;?> <book id=&quot;bk106&quot;> Randall, Cynthia Lover Birds Romance 4.95   <book id=&quot;bk107&quot;> Vinzovskaia, Irina Piano Fort A      Romance 4.95                    </li> On the File menu, click Save.</li> 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.</li></ol>

back to the top

Steps to Create the Visual C# .NET Application
 Start Visual Studio .NET, and then create a new Visual C# Console Application project.</li>  Add the following code to the top of the Code window: using System; using System.Xml; using System.IO; using System.Data ; </li>  Copy and paste the following code in static void Main(string[] args): try {       XmlTextReader xmlreader1 = new XmlTextReader(&quot;C:\\Books1.xml&quot;); XmlTextReader xmlreader2 = new XmlTextReader(&quot;C:\\Books2.xml&quot;);

DataSet ds = new DataSet; ds.ReadXml(xmlreader1); DataSet ds2 = new DataSet; ds2.ReadXml(xmlreader2); ds.Merge(ds2); ds.WriteXml(&quot;C:\\Books.xml&quot;); Console.WriteLine(&quot;Completed merging XML documents&quot;); }   catch (System.Exception ex) {       Console.Write(ex.Message); } Console.Read; </li> Build and run the application. Notice that the &quot;Completed merging XML documents&quot; message appears in the Console window.</li> Close the Console window. Notice that the Books.xml file is created in the path that you specify.</li> Open Books.xml. Notice that the data from Books2.xml is appended to the end to Books1.xml.</li></ol>

back to the top

Merge XML Documents That Have the Same Structure
The preceding Visual C# .NET sample demonstrates the output of XML documents with the same structure.

back to the top

Merge XML Documents That Have Different Structures
  Open Books2.xml, and replace the XML with the following XML: <?xml version=&quot;1.0&quot; ?> <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> </li> Save Books2.xml.</li> 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).</li></ol>

back to the top

Merge XML Documents with Similar Structure Where Second Document Contains Additional Elements
<ol> <li> Open Books2.xml, and replace the XML with the following XML: <?xml version=&quot;1.0&quot;?> <book id=&quot;bk106&quot;> Randall, Cynthia Lover Birds Romance 4.95      <publish_date>2000-09-02</publish_date> When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled. <book id=&quot;bk107&quot;> Vinzovskaia, Irina Piano Fort A      Romance 4.95      <publish_date>2000-11-02</publish_date> Two young pianists strike a chord when they fight a      pitched battle to stop the metro gnomes at a key base. </li> <li>Save Books2.xml.</li> <li>Run the Visual C# .NET project again. Notice that the nodes from the second document are appended to the first XML document.</li></ol>

back to the top

Merge XML Documents with Similar Structure Where Second Document Contains Attributes
<ol> <li> Open Books2.xml, and replace the XML with the following XML: <?xml version=&quot;1.0&quot;?> <book id=&quot;bk106&quot; genre=&quot;Romance&quot;> Randall, Cynthia Lover Birds 4.95   <book id=&quot;bk107&quot; genre=&quot;Romance&quot;> Vinzovskaia, Irina Piano Fort A      4.95 <book id=&quot;bk108&quot; genre=&quot;Horror&quot;> de Dogg, Jazz Night of the Flea 4.95                    </li> <li>Save Books2.xml.</li> <li>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.</li></ol>

back to the top

Merge XML Documents with Similar Structure Where First Document Contains Attributes
<ol> <li> Modify the Visual C# code as follows so that Books1.xml is appended to Books2.xml: try {       XmlTextReader xmlreader1 = new XmlTextReader(&quot;C:\\Books2.xml&quot;); XmlTextReader xmlreader2 = new XmlTextReader(&quot;C:\\Books1.xml&quot;);

DataSet ds = new DataSet; ds.ReadXml(xmlreader1); DataSet ds2 = new DataSet; ds2.ReadXml(xmlreader2); ds.Merge(ds2); ds.WriteXml(&quot;C:\\Books.xml&quot;); Console.WriteLine(&quot;Completed merging XML documents&quot;); }   catch (System.Exception ex) {       Console.Write(ex.Message); } Console.Read; </li> <li>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 &quot;genre&quot; 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.</li></ol>

back to the top

<div class="references_section">