21 June 2010

The node to be inserted is from a different document context

If you’re receiving this error, you’re probably trying to merge one XML node into another.  Your code could look like this:
XmlDocument xmlDoc = new XmlDocument();

...

xmlDoc.Load(strXmlFilePath);

XmlNode nodParent = xmlDoc.SelectSingleNode(strParentXPath);
XmlNode nodFragment = xes.GetXmlNode(strFragmentXml).FirstChild;

...

nodParent.AppendChild(nodFragment);
You could even be trying .Clone()
XmlDocument xmlDoc = new XmlDocument();
...
xmlDoc.Load(strXmlFilePath);
XmlNode nodParent = xmlDoc.SelectSingleNode(strParentXPath);
XmlNode nodFragment = xes.GetXmlNode(strFragmentXml).FirstChild;
...
nodParent.AppendChild(nodFragment.Clone());
But alas… the .ImportNode() is what is needed…
XmlDocument xmlDoc = new XmlDocument();
...
xmlDoc.Load(strXmlFilePath);
XmlNode nodParent = xmlDoc.SelectSingleNode(strParentXPath);
XmlNode nodFragment = xes.GetXmlNode(strFragmentXml).FirstChild;
...
nodParent.ImportNode(nodFragment.Clone(), true);



Cheers
C

No comments:

Post a Comment

Comments are moderated only for the purpose of keeping pesky spammers at bay.

Microsoft Authentication Library (MSAL) Overview

The Microsoft Authentication Library (MSAL) is a powerful library designed to simplify the authentication process for applications that conn...