Dom XML document elements replicate each other solution

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.