AppendChild of diff document

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

Trying to select a node 'E' from XML document 'ParentXML' and append the
same to another xml document 'ChildXML'


string MyXMLStr="<R><E A='1'/></R>";
XmlDocument ParentXML= new XmlDocument();
ParentXML.LoadXml(MyXMLStr);




XmlDocument ChildXML = new XmlDocument();
ChildXML.LoadXml("<R/>");

XmlElement tempE = (XmlElement) ParentXML.DocumentElement.SelectSingleNode
("E");

ChildXML.DocumentElement.AppendChild(tempE);


throws 'The node to be inserted is from a different document context.'
error.

Can I use Clone() or CloneNode() to achieve this.
A sample will be usefull.

hanks,
praveen
 
Praveen said:
Trying to select a node 'E' from XML document 'ParentXML' and append the
same to another xml document 'ChildXML'


string MyXMLStr="<R><E A='1'/></R>";
XmlDocument ParentXML= new XmlDocument();
ParentXML.LoadXml(MyXMLStr);




XmlDocument ChildXML = new XmlDocument();
ChildXML.LoadXml("<R/>");

XmlElement tempE = (XmlElement) ParentXML.DocumentElement.SelectSingleNode
("E");

ChildXML.DocumentElement.AppendChild(tempE);

You need to use ImportNode
ChildXML.DocumentElement.AppendChild(ChildXML.ImportNode(tempE, true));
 
Back
Top