Solved

Adding XmlNode from one XmlDoc to another

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now