The error message is as follows:
The node to being inserted is different the document context
It means that you cannot directly insert elements from other XML documents. The solution is to use the ImportNode () function to guide out. XmlDocument xmlold = new today, when using XML nodes to replicate, there are some problems that are wasting 2 hours of time. However, the company's network is slow, only through MSN please friends to help me according to error information to find. Fortunately, we found a way to solve the problem.
| The code is as follows |
Copy Code |
XmlDocument ()/Old document XmlNode nodeold = Xmlold.selectsinglenode ("XPath");//elements in old documents XmlDocument xmlnew = new XmlDocument (); XmlNode nodenew = Xmlnew.importnode (Nodeold, true);//First import elements from old documents Xmlnew.appendchild (Nodenew), and/or append Xmlnew.appendchild (Nodeold); |
This prompts for the error message that might be useful:
The error message is as follows:
The node to being inserted is different the document context
I tested the Clone/clonenode two methods, and the same error occurred.
Use ImportNode to solve the problem, as described in the documentation.
An experienced friend, tell me why?
-----------------------------------------------------------------------------------------------------
This is a post in English:
| The code is as follows |
Copy Code |
| Hi I ' ve got the following piece of code that ' s causing me a bit of a problem xmldocument doc = new XmlDocument (); xmlnode rootnode = Doc. createnode (xmlnodetype.element, "usersettings", Null); user user = new user (userid); xmlnode rolesnode = user. getroles (); rootnode.appendchild (Rolesnode); : : The last line (Rootnode.appendchild (Rolesnode)) throws a exception "the node to being inserted is different the document context. "How can I work Around this? The code above is Web-service to collects various Information regarding a user from different sources all which returns an XmlNode. The web-service adds each XmlNode to a "root" node and then returns This ' root ' node to the client. Eirik M. |
User's reply:
| The code is as follows |
Copy Code |
| The proble you have relates to the fact which the user object is Creating a node from othe document, rather than from Doc. Either you and Doc to getroles so this nodes are created from the Correct instance, or you could have to use ImportNode Rootnode.appendchild (Doc. ImportNode (rolesnode,true)); Instead of the previous appendchild. Best regards Ricardo |
-------------------------------------------------------------------------------------------------------
In. NET, a node has a context and can only belong to an XML document, and you can see that XmlNode has a read-only ownerdocument attribute, which is consistent with the standards of the consortium.
If you need to copy a node to another document, you need to use the ImportNode
Reference
http://msdn.microsoft.com/msdnmag/issues/02/05/web/