Solved

Adding XmlNode from one XmlDoc to another

Posted on 2003-10-24
1
347 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Convert Silverlight ERP To Angularjs,HTML5 3 109
"The resource cannot be found" 1 44
ASP.NET data base connection 35 95
convert Absolute path to relative path of FTP location 7 51
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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