Can an XmlNode from one XmlDocument be copied to another XmlDocument?

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

I'm wondering if one node that belongs to one XmlDocument can be appended to
another XmlDocument as a new child without having to build a new node from
scratch. I would think XmlNode.Clone() is for this purpose but no, it
doesn't work. I got the following error:

The node to be inserted is from a different document context

Does anyone know if this is doable without building a new node and without
putting through XSL?
 
you have to build a new node and create your own copy function. if
performance is not the top issue, use the InnerXml/OuterXml properties to
copy.

-- bruce (sqlwork.com)
 
Bob said:
I'm wondering if one node that belongs to one XmlDocument can be appended to
another XmlDocument as a new child without having to build a new node from
scratch. I would think XmlNode.Clone() is for this purpose but no, it
doesn't work. I got the following error:

The node to be inserted is from a different document context

You have to import a node before inserting it:

XmlNode newBook = doc.ImportNode(doc2.DocumentElement.LastChild, true);
doc.DocumentElement.AppendChild(newBook);
 
Back
Top