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.

SharePoint Remote Event Receivers are DEAD!!!

 Well, the time has finally come.  It was evident when Microsoft started pushing everyone to WebHooks, but this FAQ and related announcement...