DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Rob has posted 5 posts at DZone. View Full User Profile

Moving Nodes From One XmlDocument To Another

03.19.2007
| 8310 views |
  • submit to reddit
        In .NET has some powerful tools to deal with Xml. All Xml nodes need to be created in the context of a XmlDocument object. If you need to move the nodes from one Xml Document to another, it's not obvious that you can just copy the nodes from one XmlDocument to another, after all Xml is just text isn't it.

Here's how to move all the nodes from one Xml Document to another:

// Dont forget this one:
using System.Xml;

// First, get some documents together
XmlDocument DocumentSource = new XmlDocument();
XmlDocument DocumentDestination = new XmlDocument();

// Load the documents with some nodes
DocumentSource.LoadXml("");
DocumentDestination.LoadXml("");

// Now to do the conversion
XmlNode tempNode = DocumentDestination.ImportNode( DocumentSource.FirstChild )

// Now insert the fragment into the document
DocumentDestination.FirsChild.AppendChild(tempNode);