|
-
Aug 23rd, 2011, 03:53 PM
#1
Thread Starter
New Member
How to copy XML nodes from one document to another?
Hi all,
I hope someone can steer me on the right track for resolving this. Basically, all I want to do is create a node list from one xmldocument and then insert the result of that xpath query into another xmldocument at a specific place. I thought this would be quite straightforward but I'm running into problems re the node attributes. This is what the xml looks like for the source and destination files
Code:
Source File :
<?xml version="1.0" encoding="UTF-8"?>
<Application version="1.5" hwrEnabled="true">
<Name />
<Description />
<Pages />
<Item page="1" type="" boxes="" group="" checkedvalue="" left="" top="" name="">
<Settings presentation="" label="" x="" y="0" width=""/>
<Resource type="" name="" custom=""/>
</Item>
<Item page="1" type="" boxes="" group="" checkedvalue="" left="" top="" name="">
<Settings presentation="" label="" x="" y="0" width=""/>
<Resource type="" name="" custom=""/>
</Item>
</Application>
Destination File:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<settings xmlns="http://www.company.com/print">
<processors>
<object />
<object />
<object />
<object />
</processors>
<layout />
</settings>
</root>
What I'm trying to do is copy the <Item> nodes from the source to the <layout> element in the destination file so I end up with this :
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<settings xmlns="http://www.company.com/print">
<processors>
<object />
<object />
<object />
<object />
</processors>
<layout>
<Item page="1" type="" boxes="" group="" checkedvalue="" left="" top="" name="">
<Settings presentation="" label="" x="" y="0" width=""/>
<Resource type="" name="" custom=""/>
</Item>
<Item page="1" type="" boxes="" group="" checkedvalue="" left="" top="" name="">
<Settings presentation="" label="" x="" y="0" width=""/>
<Resource type="" name="" custom=""/>
</Item>
</layout>
</settings>
</root>
I started with defining a nodelist for the items and a node position for the layout tag, but at this point I start to scratch my head thinking about what is the right way to do this. I'm pretty sure I should be using a combination of appendchild and importnode but I'm not sure if this can be done as a single 'move all' kind of manner or whether I need to loop through the elements in the nodelist along with their respective attributes and copy them over individually:
Code:
Dim source As New XmlDocument
source.Load("c:/source.xml")
Dim destination As New XmlDocument
destination.Load("c:/destination.xml")
Dim items As XmlNodeList = source.SelectNodes("/Application/Item")
Dim layout As XmlNode = destination.SelectSingleNode("/root/settings/layout")
'code to copy items to layout
Can anyone give me a pointer as to an efficient way to achieve this?
Thanks
Last edited by storrm; Aug 29th, 2011 at 03:16 AM.
-
Aug 23rd, 2011, 03:59 PM
#2
Re: How to copy XML nodes from one document to another?
simple .... loop through your items (hint: for each ... in ...) and append each node (hint layout.append, might be .AppendChild ... or Layout.Child[ren].Append...) to the layout node.
-tg
-
Aug 24th, 2011, 06:44 AM
#3
Thread Starter
New Member
Re: How to copy XML nodes from one document to another?
Thanks for the push :-) Initially I used the following code but received a "The node to be inserted is from a different document context" error
Code:
For Each n As XmlNode In items
layout.AppendChild(n)
Next
Doing some research I found that the problem was to do with trying to copy nodes from one xml document to another. The answer was quite simple; use AppendChild with ImportNode :-)
Code:
For Each n As XmlNode In items
layout.AppendChild(destination.ImportNode(n, True))
Next
Thanks again for the help.
-
Aug 29th, 2011, 03:09 AM
#4
Thread Starter
New Member
Re: [RESOLVED] How to copy XML nodes from one document to another?
Unfortunately, this didnt work 100% as I had original thought. Due to the length of the xml nodes on screen, I didnt initially see that a blank xmlns="" attribute was being added to the end of each of my imported nodes so I ended up with this :-
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<settings xmlns="http://www.company.com/print">
<processors>
<object />
<object />
<object />
<object />
</processors>
<layout>
<Item page="1" type="" boxes="" group="" checkedvalue="" left="" top="" name="" xmlns="">
<Settings presentation="" label="" x="" y="0" width=""/>
<Resource type="" name="" custom=""/>
</Item>
<Item page="1" type="" boxes="" group="" checkedvalue="" left="" top="" name="" xmlns="">
<Settings presentation="" label="" x="" y="0" width=""/>
<Resource type="" name="" custom=""/>
</Item>
</layout>
</settings>
</root>
I'm guessing that because the nodes I'm importing don't utilise a namespace and that the node they are being imported into does, vb adds the blank namespace. So how do I tell it not to? I can't see any options in either the ImportNode or AppendChild methods that allow me to specify the namespace.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|