Results 1 to 4 of 4

Thread: [2005] Please help delete xml entries!

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    43

    [2005] Please help delete xml entries!

    Hi gurus.. Let me try this one more time, as I havent gotten much response on the last few questions. I have a form that contains a few text boxes, a couple of buttons, and a listbox.

    This listbox is populated from an xml file upon loading the form. When clicking on one of the items in the listbox my textboxes are populated from the corrospoding xml nodes depending on which item is clicked in the listbox. With much consternation I was able to add a button that clears the textboxes, and another button that allows the user to enter info into the textboxes and click to save the items to the xml- the new item then shows up in the listbox.

    The question I have is this: How do I apply a delete button that will delete an entry from the listbox? I tried using the removeall() and it seemed to delete everything from the xml file instead of the selected node. I attempted to use the removechild(), but that seems to do nothing. Below is the xml:

    <?xml version="1.0"?>
    <Sessionlist>
    <session name="rx30">
    <sessionname>rx30</sessionname>
    <sessionhost>192.168.0.185</sessionhost>
    <sessionuser>user</sessionuser>
    <sessionpass>pass</sessionpass>
    </session>
    <session>
    <sessionname>rx30extra</sessionname>
    <sessionhost>192.168.0.200</sessionhost>
    <sessionuser>user</sessionuser>
    <sessionpass>pass</sessionpass>
    </session>
    </Sessionlist>

    from this xml file the listbox has 2 items rx30 and rx30extra. When i click on rx30 the textboxes are populated with the sessionpass, sessionuser, sessionname. There is a delete button, and I want to be able to click it and delete the entire entry from <session> to </session> with the corrosponding <sessionname> which was selected from the listbox. Below is the code to populate the list, and display the textboxes:

    Code:
        Dim xmlfile As String = "c:\sessionlist.xml"
        Dim xmldoc As New XmlDocument
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PopulateList()
    
        End Sub
        Private Sub PopulateList()
            xmldoc.Load(xmlfile)
            Dim openfile As New XmlTextReader(xmlfile)
    
            While openfile.Read
                If openfile.Name = "sessionname" AndAlso openfile.NodeType = XmlNodeType.Element Then
                    ListBox1.Items.Add(openfile.ReadString)
                End If
            End While
            openfile.Close()
        End Sub
    
    
        Sub DisplayHosts(ByVal position As Integer)
            Dim node As XmlNode = xmldoc.SelectSingleNode( _
                    "/Sessionlist/session[" & position & "]")
            hostname.Text = node.SelectSingleNode("sessionhost").InnerText
            userbox.Text = node.SelectSingleNode("sessionuser").InnerText
            pwbox.Text = node.SelectSingleNode("sessionpass").InnerText
            sessionname.Text = node.SelectSingleNode("sessionname").InnerText
    
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            DisplayHosts(ListBox1.SelectedIndex + 1)
    
        End Sub
    Please help!!! If you have advise on editing the xml entries, that would be wonderful as wel!

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    43

    Re: [2005] Please help delete xml entries!

    OK, i managed to get them to delete.. funny how writing about it makes it become clear.. I added a subroutine :
    Code:
    Sub DeleteHosts(ByVal position As Integer)
            Dim delnode As XmlNode = xmldoc.SelectSingleNode( _
                    "/Sessionlist/session[" & position & "]")
            delnode.RemoveAll()
            xmldoc.Save("c:\sessionlist.xml")
            
        End Sub
    Its leaving behind an empty list:
    <session>
    </session>

    any help with that, and being able to edit would be awesome!!

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    43

    Re: [2005] Please help delete xml entries!

    Looks like i fixed that too, just by changing my delete code to the following:

    Code:
     Sub DeleteHosts(ByVal position As Integer)
            Dim iResponse As Integer
            iResponse = MsgBox("Are you sure you want to delete the session?", vbYesNo, "Confirm Session Delete")
            If iResponse = vbYes Then
                Dim delnode As XmlNode = xmldoc.SelectSingleNode( _
                        "/Sessionlist/session[" & position & "]")
                Dim root As XmlNode = xmldoc.DocumentElement
                root.RemoveChild(delnode)
                xmldoc.Save("c:\sessionlist.xml")
            Else
                Return
            End If
    I also obviously added a warning button as well.. if someone can help me with a way to edit these entries I would appreciate it. I realize this is all pretty basic, and it may seem 'beneath you' to respond, but I would greatly appreciate it..

  4. #4
    New Member
    Join Date
    Nov 2007
    Posts
    1

    Re: [2005] Please help delete xml entries!

    Wow thanks so much. I spent about 8 hours pulling my hair out trying to figure out how to delete the child and parent nodes. So hopefully thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width