adding elements of XmlDocument A in XmlDocument B

  • Thread starter Thread starter =?ISO-8859-15?Q?Martin_P=F6pping?=
  • Start date Start date
?

=?ISO-8859-15?Q?Martin_P=F6pping?=

Hello,

I want to add some elements of one XmlDocument in another.
My code looks like this:

XmlDocument docA= new XmlDocument();
docA.LoadXml(record);
XmlNodeList nl = docA.SelectNodes("/search/recordlist/record");

XmlNode recordlist = docB.FirstChild;

foreach (XmlNode node in nl)
{
recordlist.AppendChild(node);
}


But these lines of code do not work,
because the compiler tells me:

"The node to be inserted is from a different document context."


How can I solve this problem?



Regards,

Martin
 
Martin said:
Hello,

I want to add some elements of one XmlDocument in another.
My code looks like this:

XmlDocument docA= new XmlDocument();
docA.LoadXml(record);
XmlNodeList nl = docA.SelectNodes("/search/recordlist/record");

XmlNode recordlist = docB.FirstChild;

foreach (XmlNode node in nl)
{
recordlist.AppendChild(node);

You need to use ImportNode
recordList.AppendChild(recordlist.OwnerDocument.ImportNode(node,
true));


Note that ImportNode will create a copy of the node to be imported so
the code above will not remove the nodes from document A.
 
Back
Top