Results 1 to 4 of 4

Thread: How to copy XML nodes from one document to another?

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Unhappy 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width