By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

Getting exception "The node to be inserted is from a different document context."

P: n/a
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 an exception "The
node to be inserted is from a different document context." How can I work
around this? The code above is from a Web-service that collects various
information regarding a user from different sources each of 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.
Nov 11 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The proble you're having relates to the fact that the user object is
creating a node from othe document, rather than from doc.

Either you pass doc to GetRoles so that the nodes are created from the
correct instance, or you may have to use ImportNode

rootNode.AppendChild(doc.ImportNode(rolesNode,true ));

instead of the previous AppendChild.

best regards
ricardo

"Eirik M." <eirik_at_stockpoint_dot_no> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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 an exception "The
node to be inserted is from a different document context." How can I work
around this? The code above is from a Web-service that collects various
information regarding a user from different sources each of 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.

Nov 11 '05 #2

P: n/a
Hi, Ricardo

Thanks for your quick reply.

"news.microsoft.com" <ricardo pereira> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
The proble you're having relates to the fact that the user object is
creating a node from othe document, rather than from doc.

Either you pass doc to GetRoles
Nah, don't wan't to pass it around.
so that the nodes are created from the
correct instance, or you may have to use ImportNode

rootNode.AppendChild(doc.ImportNode(rolesNode,true ));
Did the trick!

instead of the previous AppendChild.

best regards
ricardo
Eirik M ^_^

"Eirik M." <eirik_at_stockpoint_dot_no> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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 an exception "The node to be inserted is from a different document context." How can I work around this? The code above is from a Web-service that collects various
information regarding a user from different sources each of 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.


Nov 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.