Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove a XML node in VB.Net

Status
Not open for further replies.

jkusmanto

MIS
Feb 27, 2006
5
BE
Hi all,

I have a problem regarding XML file.
I try to manipulate it in ASP.Net/VB.Net.

I have a XML file (oefenPizzas.xml) like this :
<?xml version="1.0" encoding="utf-8" ?>
<pizzas>
<pizza prijs="6">
<name>Napoletana</name>
<topping>tomaten</topping>
<topping>kaas</topping>
<topping>ansjovis </topping>
</pizza>
<pizza prijs="8">
<name>Tonno</name>
<topping>tomaten</topping>
<topping>kaas</topping>
<topping>tonijn</topping>
<topping>uien</topping>
</pizzas>

Let's say I want to delete topping "kaas" in pizza "Tonno". How to do this ?

What I have done is :
I have 1 button and 2 listboxes on my form.
First listbox (loaded in Page_Load) I fill it with the name of Pizzas.
When the name is clicked, let's say "Tonno" is clicked, I fill all the toppings of "Tonno" in the 2nd listbox.
User has to choose one of the toppings which he want to delete (let's say "tomaten"). And click the button.
When the button click, the choosen topping has to delete from XML file.

My code is :
-----------------------------------
Private Sub Page_Load .......
If Not Me.IsPostBack Then
Dim dok As New XmlDocument
dok.Load(Me.Server.MapPath("oefenPizzas.xml"))
Dim piz As XmlElement = dok.DocumentElement
Dim pn As XmlNode
For Each pn In piz.SelectNodes("pizza")
lbxPizza.Items.Add(pn.SelectSingleNode("name/text()").Value)
Next
End If
End Sub

Private Sub lbxPizza_SelectedIndexChanged ........
lbxTopping.Items.Clear()
Dim dok As New XmlDocument
dok.Load(Me.Server.MapPath("oefenPizzas.xml"))
Dim piz As XmlElement = dok.DocumentElement
Dim top As XmlNode
For Each top In piz.SelectNodes("pizza[name='" & _
lbxPizza.SelectedValue & "']/topping/text()")
lbxTopping.Items.Add(top.Value)
Next
End Sub

Private Sub btnRemove_Click ......
If lbxTopping.SelectedValue <> Nothing Then
Dim dk As New XmlDocument
dk.Load(Me.Server.MapPath("oefenPizzas.xml"))
Dim piz As XmlElement = dk.DocumentElement
Dim topp As XmlNode
For Each topping In piz.SelectNodes("pizza[name='" & _
lbxPizza.SelectedValue & "']/topping/text()")
If topp.Value = lbxTopping.SelectedValue Then
topp.ParentNode.RemoveChild(topp)
dk.Save(Me.Server.MapPath("oefenPizzas.xml"))
lbxTopping.Items.Remove(lbxTopping.SelectedValue)
Exit For
End If
Next
End If
End Sub
------------------------------
The result of the code is :

<?xml version="1.0" encoding="utf-8" ?>
<pizzas>
<pizza prijs="6">
<name>Napoletana</name>
<topping>tomaten</topping>
<topping>kaas</topping>
<topping>ansjovis </topping>
</pizza>
<pizza prijs="8">
<name>Tonno</name>
<topping>
</topping>
<topping>kaas</topping>
<topping>tonijn</topping>
<topping>uien</topping>
</pizzas>
---------------
The value ("Tomaten") is gone, but the node still there.
Can anybody help me to solve this problem, please ?

Thanks in advance and regards,

Juliando
 
This might have something to do with it:

Dim [red]topp[/red] As XmlNode
For Each [red]topping[/red] In piz.SelectNodes("pizza[name='" & _
lbxPizza.SelectedValue & "']/topping/text()")

You Dim a variable called 'topp', then use a variable called 'topping' in the loop. I think the text is being removed when you do the lbxTopping.Items.Remove, but the node is not being deleted because the value of 'topp' is not changing as the loop iterates. Try changing the code to this

Dim [red]topp[/red] As XmlNode
For Each [red]topp[/red] In piz.SelectNodes("pizza[name='" & _
lbxPizza.SelectedValue & "']/topping/text()")

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top