How to remove an XML Node

M

mark

Hi, I am having trouble with deleting nodes from an XML file. I use the
following code to remove an XML node.

xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
ListBox1.SelectedItem.ToString & "']")
xNode.RemoveAll()

<country name="France">
<city name="Paris">
<street name="1 France Lane">
<details>
<postcode>9988jj</postcode>
<province></province>
<c>France</c>
</details>
</street>
<street name="2 France Lane">
<details>
<postcode>9988jj</postcode>
<province></province>
<c>France</c>
</details>
</street>
</country>

The code seems to work except it is not removing the full node. I.E it
leaves behind and empty tag.

For example, if I needed to delete the second street address in the XML
above (2 France Lane) it would leave an empty tag of <street></street>
behind as illustrated below.

<country name="France">
<city name="Paris">
<street name="1 France Lane">
<details>
<postcode>9988jj</postcode>
<province></province>
<c>France</c>
</details>
</street>
<street>
</street>
</country>

Can anyone advise how I can clean this up and remove the full Street
element?

Many thanks.

Mark.
 
M

Martin Honnen

mark said:
Hi, I am having trouble with deleting nodes from an XML file. I use the
following code to remove an XML node.

xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
ListBox1.SelectedItem.ToString & "']")
xNode.RemoveAll()

RemoveAll remove attributes and children but not the node itself. To do
that use e.g.
xNode.ParentNode.RemoveChild(xNode)
 
M

mark

Excellent, thanks Martin that worked.

Martin said:
mark said:
Hi, I am having trouble with deleting nodes from an XML file. I use
the following code to remove an XML node.

xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
ListBox1.SelectedItem.ToString & "']")
xNode.RemoveAll()

RemoveAll remove attributes and children but not the node itself. To do
that use e.g.
xNode.ParentNode.RemoveChild(xNode)
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top