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
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: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>
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:<?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>
Can anyone give me a pointer as to an efficient way to achieve this?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
Thanks




Reply With Quote