error : "The node to be inserted is from a different document context."

i have 2 forms (2 different classes in same namespace in Visual in a C# program.
In the first form, i read XML file and push xmlnode into Stack, then pass the Stack to second form's constructor(i copied stack to second form)  
In the second form, i pop  xmlnode from the copied Stack. I create a new xmlnode. I tried to append child the new xmlnode to the xmlnode of stack, like this:  xmlnode_of_stack.appendchild(new xmlnode);

Compile is ok
But when i run at that line, i got this error "The node to be inserted is from a different document context."
I'm a new in C#.
I will accept any solution can help me out>>>

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this, or whatever variant of it you need:

XmlNode newNode = xmlnode_of_stack.OwnerDocument.CreateElement("X");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
therock_80Author Commented:
I already created new node that is parent of other new childs node along with data.
After i created this parent node, i want to append it to xmlnode_of_stack.
But you showed that you can only create new node in CreateElement method(not with parent node i created).
Also, when i tried these code;
XmlNode newNode = xmlnode_of_stack.OwnerDocument.CreateElement("X");
, xml document was not updated. Every nodes of xml document are pushed into stack of owner form. Then i passed this stack to its child form, so when i update the nodes in the stack, will it update xml document.

Here is passing stack i did in constructor of child form1:
private Stack form2Stack;
public form1(stack form1Stack)
  form2Stack = form1Stack;
I think it will be passsed by ref. So any time i modify nodes in form2Stack, will it automatically updated in stack of form1?

Thank yoiu
After adding the node, are you pushing it back onto the stack in form2, and then popping it back off in form 1?
Clone the node first or remove it from the original document. A node cannot be inserted in two documents at the same time. (Note: I did not read the whole thread, may be it was already mentioned before...)
therock_80Author Commented:
well i made it work.
Its so simple. I just make xmldocument is private in Form1. Then i passed it to Form2
So i guess i will give 50 points for the guy answer me
Thanks all
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.