AppendChild removes like nodes?

G

Guest

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?
 
B

bruce barker

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)


| 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?
|
|
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top