Microsoft KB Archive/286817

= How To Merge Data in Two XML Documents by Programming the Document Object Model (DOM) =

Article ID: 286817

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft XML Parser 2.0
 * Microsoft XML Parser 2.5
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 4.0
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 3
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 4
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q286817



SUMMARY
This article documents a Visual Basic code sample that demonstrates how to program the MSXML Document Object Model (DOM) to merge the data that is contained in two identical XML documents.



MORE INFORMATION
Merging data that is contained in two or more identical XML documents into a single XML document is a common requirement. The code and method that is demonstrated in this article can be further expanded to merge three or more XML documents. If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:

305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds

 Open an empty text file in Microsoft Notepad.  Copy and paste the following XML code into Notepad, and then save the file as xmlbooks1.xml:    XML Step By Step MS Press   Developing XML Solutions</Title> MS Press</Publisher> </Book> </Books> </li> Open a New text file in Notepad.</li>  Copy and paste the following XML code into Notepad, and then save the file as xmlbooks2.xml: <?xml version=&quot;1.0&quot;?> <Books>  Beginning XML</Title> Wrox</Publisher> </Book>  Professional XML</Title> Wrox</Publisher> </Book> </Books> </li> Open a new Standard EXE project in Visual Basic 6.0. Form1 is created by default.</li> On the Project menu, set a project reference to Microsoft XML, v3.0 or later.</li> Place a Command Button onto Form1. Command1 is created by default.</li>  Copy and paste the following code into the Click event procedure of the CommandButton: ' In the ProgIDs below, change 30 to reflect the installed version of the Microsoft XML Parser. ' For example, use Dim doc1 As MSXML2.DOMDocument40 for MSXML 4.

Dim doc1 As MSXML2.DOMDocument30 Dim doc2 As MSXML2.DOMDocument30 Dim doc2Node As MSXML2.IXMLDOMNode

Set doc1 = New MSXML2.DOMDocument30 Set doc2 = New MSXML2.DOMDocument30

doc1.Load &quot;d:\xmlbooks1.xml&quot; doc2.Load &quot;d:\xmlbooks2.xml&quot;

For Each doc2Node In doc2.documentElement.childNodes doc1.documentElement.appendChild doc2Node Next

MsgBox doc1.xml

doc1.save &quot;d:\AllXMLBooks.xml&quot; </li> The preceding code loads the two XML documents that are created in steps 1 through 4 into two MSXML DOMDocument objects. The code then merges the data in xmlbooks2.xml into the instance of the DOMDocument object containing the data in xmlbooks1.xml. This is achieved by looping through the child nodes of the document element of the doc2 DOMDocument object, and appending each of them to the document element of the doc1 DOMDocument. Finally, the merged XML is displayed in a Message box and persisted to a file on disk.</li>  Open AllXMLBooks.xml in Microsoft Internet Explorer to view the merged XML document, and note the following XML: <?xml version=&quot;1.0&quot;?> <Books> <Book> <Title>XML Step By Step</Title> <Publisher>MS Press</Publisher> </Book> <Book> <Title>Developing XML Solutions</Title> <Publisher>MS Press</Publisher> </Book> <Book> <Title>Beginning XML</Title> <Publisher>Wrox</Publisher> </Book> <Book> <Title>Professional XML</Title> <Publisher>Wrox</Publisher> </Book> </Books> </li></ol>

Additional query words: IE

Keywords: kbhowto KB286817

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.