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

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
mark - 14 Jun 2007 14:40 GMT
Excellent, thanks Martin that worked.
>> Hi, I am having trouble with deleting nodes from an XML file. I use
>> the following code to remove an XML node.
[quoted text clipped - 6 lines]
> that use e.g.
> xNode.ParentNode.RemoveChild(xNode)