AppendChild removes like nodes?

Discussion in 'ASP .Net' started by =?Utf-8?B?YmVhbndlZWQ=?=, Mar 1, 2005.

  1. This is a problem with a ASP.NET application I am writing.
    The interface allows a user to manipulate an XML document.
    I have in my xmldocument

    .. . .
    <item id="1">
    <parents />
    <label>item1</label>
    </item>
    <item id="3">
    <parents>
    <parent>1</parent>
    </parents>
    <label>item3</label>
    </item>
    <item id="2">
    <parents />
    <label>item2</label>
    </item>
    .. . .

    In short, item3 is a "child" of item1 but item2 is a "peer".
    Code to allow a user to cut and paste items tries to read the
    parent information from the item being pasted to, and then
    add that information to the item being pasted.
    For example if I wanted to cut out item2 and paste it in as
    a "child" of item3 I should get


    .. . .
    <item id="1">
    <parents />
    <label>item1</label>
    </item>
    <item id="3">
    <parents>
    <parent>1</parent>
    </parents>
    <label>item3</label>
    </item>
    <item id="2">
    <parents>
    <parent>1</parent>
    <parent>3</parent>
    </parents>
    <label>item2</label>
    </item>
    .. . .

    (i.e. item2 is now a "child" of item3 which is also a "child" of item1),
    but what I get is

    .. . .
    <item id="1">
    <parents />
    <label>item1</label>
    </item>
    <item id="3">
    <parents/>
    <label>item3</label>
    </item>
    <item id="2">
    <parents>
    <parent>1</parent>
    <parent>3</parent>
    </parents>
    <label>item2</label>
    </item>
    .. . .

    By writing choice lines to a text file I find that the problem lies
    in this line of code

    cutparentnode.AppendChild(selparentnode)

    Apparently, when I append the element <parent>1</parent> to item2
    it removes that same element from item3. I see the documetation
    says "If the newChild is already in the tree, it is first removed."
    I thought it meant if the newchild is in the tree of the node being
    appended and its children. But I guess it means in the tree of the
    document?

    Whatever. How do I append an element to a node without having the
    same element removed elsewhere in the document?
     
    =?Utf-8?B?YmVhbndlZWQ=?=, Mar 1, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?YmVhbndlZWQ=?=

    bruce barker Guest

    in xml a node can have only one parent, so it can only exist only place in
    the document. if you want to copies, you need to clone it (and maybe its
    children if desired) and paste the clone.

    -- bruce (sqlwork.com)


    "beanweed" <> wrote in message
    news:...
    | This is a problem with a ASP.NET application I am writing.
    | The interface allows a user to manipulate an XML document.
    | I have in my xmldocument
    |
    | . . .
    | <item id="1">
    | <parents />
    | <label>item1</label>
    | </item>
    | <item id="3">
    | <parents>
    | <parent>1</parent>
    | </parents>
    | <label>item3</label>
    | </item>
    | <item id="2">
    | <parents />
    | <label>item2</label>
    | </item>
    | . . .
    |
    | In short, item3 is a "child" of item1 but item2 is a "peer".
    | Code to allow a user to cut and paste items tries to read the
    | parent information from the item being pasted to, and then
    | add that information to the item being pasted.
    | For example if I wanted to cut out item2 and paste it in as
    | a "child" of item3 I should get
    |
    |
    | . . .
    | <item id="1">
    | <parents />
    | <label>item1</label>
    | </item>
    | <item id="3">
    | <parents>
    | <parent>1</parent>
    | </parents>
    | <label>item3</label>
    | </item>
    | <item id="2">
    | <parents>
    | <parent>1</parent>
    | <parent>3</parent>
    | </parents>
    | <label>item2</label>
    | </item>
    | . . .
    |
    | (i.e. item2 is now a "child" of item3 which is also a "child" of item1),
    | but what I get is
    |
    | . . .
    | <item id="1">
    | <parents />
    | <label>item1</label>
    | </item>
    | <item id="3">
    | <parents/>
    | <label>item3</label>
    | </item>
    | <item id="2">
    | <parents>
    | <parent>1</parent>
    | <parent>3</parent>
    | </parents>
    | <label>item2</label>
    | </item>
    | . . .
    |
    | By writing choice lines to a text file I find that the problem lies
    | in this line of code
    |
    | cutparentnode.AppendChild(selparentnode)
    |
    | Apparently, when I append the element <parent>1</parent> to item2
    | it removes that same element from item3. I see the documetation
    | says "If the newChild is already in the tree, it is first removed."
    | I thought it meant if the newchild is in the tree of the node being
    | appended and its children. But I guess it means in the tree of the
    | document?
    |
    | Whatever. How do I append an element to a node without having the
    | same element removed elsewhere in the document?
    |
    |
     
    bruce barker, Mar 1, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. asd
    Replies:
    3
    Views:
    460
    Arnaud Berger
    May 23, 2005
  2. Ethan Furman
    Replies:
    3
    Views:
    263
    Roel Schroeven
    Jun 17, 2008
  3. Terry Reedy
    Replies:
    4
    Views:
    393
    Terry Reedy
    Jun 17, 2008
  4. Beznas
    Replies:
    8
    Views:
    259
    Evertjan.
    Sep 10, 2003
  5. Christina

    Firefox doesn't like appendChild()

    Christina, Nov 19, 2006, in forum: Javascript
    Replies:
    2
    Views:
    131
    Christina
    Nov 19, 2006
Loading...

Share This Page