Solved

Adding XmlNode from one XmlDoc to another

Posted on 2003-10-24
1
339 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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

16 Experts available now in Live!

Get 1:1 Help Now