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

Posted on 2004-11-27
Last Modified: 2008-01-09
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>>>

Question by:therock_80
    LVL 16

    Accepted Solution

    Try this, or whatever variant of it you need:

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

    Author Comment

    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
    LVL 16

    Expert Comment

    After adding the node, are you pushing it back onto the stack in form2, and then popping it back off in form 1?
    LVL 10

    Expert Comment

    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...)
    LVL 1

    Author Comment

    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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now