A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
How to copy XML nodes between documents
In this tutorial we'll quickly review how you can copy an XML node (XmlNode) from one XML document to another. The most important methods we're going to use are AppendChild() and ImportNode().
On Friday, November 11th 2005 at 06:11 AM
By Andrew Pociu (View Profile)
(Rated 4.7 with 63 votes)
Download this project (Visual Studio 2005)
Very often, your first try of copying a node from an XML file to another will result in the following error:
The node to be inserted is from a different document context.
That's because you are using AppendChild() incorrectly. First you need to create a new node using ImportNode() and then add that node to the document.
In the example project attached to this tutorial and in the source code below we copy a node named CopyMe (<CopyMe>) from SourceDoc.xml to DestinationDoc.xml. Here's how the two XML documents look like:
Before we start, make sure you add the following using directive:
First we load the source XML file into an XmlDocument object:
Then we load the destination XML file into a different XmlDocument:
Then we need to store the root node of the destination document into an XmlNode, because we will later append the copy of the node to it. Next we store the node into an XmlNode (nodeOrig) and its copy into nodeDest. Using AppendChild() we append the node into the destination document.
The obvious thing to do might not be to use ImportNode() like I did here, so you might say "why can't
I use AppendChild() directly on nodeOrig instead of creating a copy of nodeOrig and putting it into nodeDest?". Well, if you do that and use AppendChild(nodeOrig) you will get the error "The node to be inserted is from a different document context". And you don't want that.
So to prevent this error you need to use ImportNode(nodeOrig, true). Also, if you're wondering what the true parameter is for - it specifies wether or not to perform a deep import (including child nodes) or not.
Finally, we write the changes to the destination XML file:
|Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
C# Job Search
From the creators of Geekpedia, a revolutionary new coupon website!
BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.