Solved

Adding XmlNode from one XmlDoc to another

Posted on 2003-10-24
1
346 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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