Solved

Adding XmlNode from one XmlDoc to another

Posted on 2003-10-24
1
348 Views
Last Modified: 2010-04-16
I want to add an XmlNode from one doc to another,

Basically, I have

one doc,

<UserDatabase>
<UserSet>
<User/>
<User/>
</UserSet>
<UserSet>
<User/
....
</UserDatabase>

The UserSets are created on their own by other methods under different XmlDocuments.  I then want to pass all of these Documents whose top node is <UserSet> and merge them into the above large XML file.  What is the easiest way to do this?  I am under the impression you cannot just get a node from one xmlDocument and add it to another because it would mess up the parent information.  So, do I need to copy all the nodes by hand, can I copy them easily?  

0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
purpleblob earned 200 total points
ID: 9665541
Not sure if this is exactly what you're after but you can use ImportNode and AppendChild to take an element from one document and place it in another - for example:

The following loads an xml document (doc1.xml) then runs an XPath statement to get some nodes back from it, next it's loads a second xml document (doc2.xml) and then places each of the nodes from

// load the first xml doc
System.Xml.XmlDocument xmldoc1 = new System.Xml.XmlDocument();
xmldoc1.Load("doc1.xml");

// load the second xml doc
System.Xml.XmlDocument xmldoc2 = new System.Xml.XmlDocument();
xmldoc2.Load("doc2.xml");

// do some processing to get the nodes we want to place from doc1 into doc2
System.Xml.XmlNodeList nodes = xmldoc.SelectNodes("//UserSet");

// loop through the nodes and import them into the second doc
foreach(System.Xml.XmlNode node in nodes)
{
   System.Xml.XmlNode src = xmldoc2.ImportNode(node, true);
   xmldoc2.DocumentElement.AppendChild(src);
}

Hope this helps
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question