RSS

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

Post a Comment

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