Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 18th, 2006, 07:27 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default To remove a XML node in VB.Net

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 (look at the red one) :

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

Reply With Quote
  #2 (permalink)  
Old May 18th, 2006, 11:08 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That's because you select the text node:
Code:
pizza[name='" & lbxPizza.SelectedValue & "']/topping/text()
It would be easier, rather than "for each" over the toppings to do:
Code:
Private Sub btnRemove_Click ......
        If lbxTopping.SelectedValue <> Nothing Then
            Dim dk As New XmlDocument
            dk.Load(Me.Server.MapPath("oefenPizzas.xml"))
            Dim topp As XmlNode
            topp = dk.SelectSingleNode("/pizzas/pizza[name = '" & _
                      lbxPizza.SelectedValue & _ 
"']/topping[. = '" & lbxTopping.SelectedValue & "']")
           If topp Is Not Nothing Then
                       topp.ParentNode.RemoveChild(topp) 
                    dk.Save(Me.Server.MapPath("oefenPizzas.xml"))
           End If
                    lbxTopping.Items.Remove(lbxTopping.SelectedValue)
        End If
That assumes that each different topping can only appear one in any one pizza. If the same topping can appear twice in one pizza then change selectsinglenode to selectNodes, loop through them and delete them one by one.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old May 19th, 2006, 06:12 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help joefawcett,
It works.

I just made a little editing.
It should be :
 If Not topp Is Nothing Then
  End If
instead of (from you) :
 If topp Is Not Nothing Then
  End If


Rgds,
Juliando


Reply With Quote
  #4 (permalink)  
Old May 19th, 2006, 06:23 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by jkusmanto
 I just made a little editing.
It should be :
 If Not topp Is Nothing Then
End If
instead of (from you) :
 If topp Is Not Nothing Then
End If

Not used to VB.NET...

I noticed you had
Code:
If lbxTopping.SelectedValue <> Nothing
Does that construct work in VB.NET?

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #5 (permalink)  
Old May 23rd, 2006, 09:18 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe,

Ofcouse it works
This statement :
If lbxTopping.SelectedValue <> Nothing
is a VB.Net statement.


Rgds,
Juliando

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.net adding data in a selected node saikoboarder XML 9 May 16th, 2008 06:37 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
How to insert a New node into the XML Document VB jabrouni1 Classic ASP XML 1 January 26th, 2007 06:11 AM
remove node asap XSLT 1 July 30th, 2006 03:01 AM
Urgent Help:Deleting a node in xml using vb : Milan XML 6 November 6th, 2004 03:28 AM



All times are GMT -4. The time now is 03:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.