Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
| Search | Today's Posts | Mark Forums Read
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2006, 09:48 AM
Authorized User
 
Join Date: Jun 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to merge two xml documents together?

Hello guys,

I am trying to merge two xml documents in VB.NET

I have one xmldocument which has the following xml:

<Allocations>
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
</Allocations>

I have another xml Document which has the following:
<Allocations>
   <Item />
   <Item />
   <Item />
</Allocations>

I want to merge the second xmldocument into the first one.

So I want the following:
<Allocations>
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
</Allocations>


The way I approaced it is as follows:

The first xmldocument is called: xdoc

The second xmldocument is called: xdoc_Selected

Dim xnodeList As XmlNodeList = xdoc_Selected.SelectNodes("//Allocations/Item")
For Each xnode In xnodeList
  xdoc.AppendChild(xnode)
Next

But this was giving me an error. so I tried this method:

xtempnode = xdoc.ImportNode(xdoc_Selected.DocumentElement, True)
xdoc.DocumentElement.AppendChild(xtempnode)

But this results in the following xml:

<Allocations>
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
   <Item />
   <Allocations>
       <Item />
       <Item />
       <Item />
   </Allocations>
</Allocations>

By the way, I DO NOT want to load the two xml's into a DataSet and then merge them. I want to do it without DataSet. Please advice me on what I am doing wrong. Thank you.

Sanch



 
Old June 15th, 2006, 04:03 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I think you are just going to have to walk the nodes, and be more rudimentary about it.
Is there an .AppendNode method that is the functional equivalent of .AppendChild? It looks as if (if there is such a creature) that would do the trick in the specific example you gave.
 
Old August 22nd, 2008, 05:28 AM
Registered User
 
Join Date: Aug 2008
Location: Auckland, , New Zealand.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rather than adding the 'DocumentElement' of the second file try something like:

Dim xnodeList As XmlNodeList = xdoc_Selected.SelectNodes("//Allocations/Item")
For Each xnode In xnodeList
  xdoc.DocumentElement.AppendChild(xdoc.ImportNode(x node))
Next





Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge PDF Documents Gyto ASP.NET 2.0 Basics 3 August 19th, 2013 02:33 AM
compare two xml documents bcogney XSLT 1 October 30th, 2006 03:33 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
merge 2 xml documents Beulah VS.NET 2002/2003 0 November 20th, 2003 09:33 PM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.