Results 1 to 10 of 10

Thread: XML: Search and Destroy

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    XML: Search and Destroy

    Here's a sample xml file that my program writes:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Movies>
      <MovieName xmlns="National Lampoons Barely Legal.avi">
        <Directory>C:\Downloads\Movies\National Lampoons Barely Legal  By Digitaljace.avi</Directory>
      </MovieName>
      <MovieName xmlns="Robin.Hood.S03E12.HDTV.XviD-BiA.avi">
        <Directory>C:\Downloads\Movies\Robin.Hood.S03E12.HDTV.XviD-BiA.avi</Directory>
      </MovieName>
      <MovieName xmlns="American.Pie.Beta.House DVDRIP.avi">
        <Directory>C:\Downloads\Movies\American Pie 1,2,3,4,5,6 DVDRIP\American.Pie.Beta.House DVDRIP.avi</Directory>
      </MovieName>
    </Movies>
    When the user press a button called Remove, the program should find the movie the user has marked in a listview (the name of the movie is the item name column 1) and delete it from the xml file.

    Say i wanna remove robin hood. I mark the item in the listview, clicks remove, it removes it from the listview (i already know how) and then removes this node : <MovieName xmlns="Robin.Hood.S03E12.HDTV.XviD-BiA.avi">
    and everything in it.

    I saw some code related to XPath, but i don't understand directly.

    It would be nice with an example.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: XML: Search and Destroy

    Actually you can use LINQ/lambdas to do this:

    vb.net Code:
    1. Public Function RemoveMovie(ByVal movieName As String) As Xml.XmlNode
    2.     Dim document As New Xml.XmlDocument()
    3.     Dim removeNode As Xml.XmlNode = Nothing
    4.     document.Load("C:\test\movies.txt") 'path to your xml
    5.  
    6.     removeNode = document.GetElementsByTagName("MovieName").OfType(Of Xml.XmlNode) _
    7.                          .Where(Function(node) node.NamespaceURI _
    8.                                     .Contains(movieName)).FirstOrDefault()
    9.  
    10.     If removeNode IsNot Nothing Then
    11.         removeNode.ParentNode.RemoveChild(removeNode)
    12.         document.Save("C:\test\movies.txt") 'save the xml with the removed node
    13.     End If
    14.  
    15.     Return removeNode
    16. End Function
    17.  
    18. Public Sub DoSomething()
    19.     Me.RemoveMovie("Robin")
    20. End Sub

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: XML: Search and Destroy

    Way to hide the fact you are downloading copyrighted material illegally off the internet.

  4. #4

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: XML: Search and Destroy

    No off topic please -.-

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: XML: Search and Destroy

    Quote Originally Posted by Cyb3rH4Xter View Post
    No off topic please -.-
    Uh... speaking of which, did you try the code?

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: XML: Search and Destroy

    Quote Originally Posted by Cyb3rH4Xter View Post
    No off topic please -.-
    You are barking up the wrong tree buddy.

  7. #7

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: XML: Search and Destroy

    yeah, the code looks like this:

    Code:
        Public Function RemoveMovie(ByVal movieName As String) As Xml.XmlNode
    
            For Each lvItem As ListViewItem In ListView2.SelectedItems
    
                lvItem.Remove()
    
                Dim document As New Xml.XmlDocument()
    
                Dim removeNode As Xml.XmlNode = Nothing
    
                document.Load(dataFile) 'path to your xml
    
                removeNode = document.GetElementsByTagName("MovieName").OfType(Of Xml.XmlNode)().Where(Function(node) node.NamespaceURI.Contains(movieName)).FirstOrDefault()
    
                If removeNode IsNot Nothing Then
    
                    removeNode.ParentNode.RemoveChild(removeNode)
    
                    document.Save(dataFile) 'save the xml with the removed node
    
                End If
    
                Return removeNode
    
            Next
    
        End Function
    
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    
            For Each lvItem As ListViewItem In ListView2.SelectedItems
    
                lvItem.Remove()
    
                RemoveMovie(lvItem.Text)
    
            Next
    
        End Sub
    But it doesn't remove anything? And it gives no errors.

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: XML: Search and Destroy

    1. Why are you looping through the ListView twice?
    2. The document should be loaded outside of the loop so that you aren't loading the document on each iteration.
    3. The "node.NamespaceURI" property will return this part of the node in double quotes:

    xml Code:
    1. xmlns="Robin.Hood.S03E12.HDTV.XviD-BiA.avi"

    So ensure that the ListViewItem's text is actually contained in there. If your ListViewItem's text is "Robin Hood" it obviously won't be removed because it's "Robin.Hood".

  9. #9

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: XML: Search and Destroy

    Thx dude!

    But there is One thing that don't work now. Removing many items. If i select like 3 items in the listview and click remove, only the first marked item get's removed from the xml file, but all gets removed from the listview. So there's something with the loop.

    Here's the code now:

    Code:
        Public Function RemoveMovie(ByVal movieName As String) As Xml.XmlNode
    
            Dim document As New Xml.XmlDocument()
    
            Dim removeNode As Xml.XmlNode = Nothing
    
            document.Load(dataFile) 'path to your xml
    
            removeNode = document.GetElementsByTagName("Moviename").OfType(Of Xml.XmlNode)().Where(Function(node) node.NamespaceURI.Contains(movieName)).FirstOrDefault()
    
            If removeNode IsNot Nothing Then
    
                removeNode.ParentNode.RemoveChild(removeNode)
    
                document.Save(dataFile) 'save the xml with the removed node
    
            End If
    
            Return removeNode
    
        End Function
    
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    
            For Each lvItem As ListViewItem In ListView2.SelectedItems
    
                lvItem.Remove()
    
                RemoveMovie(lvItem.Text)
    
            Next
    
        End Sub

  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: XML: Search and Destroy

    Try it like this:

    vb.net Code:
    1. Public Sub RemoveMovies(ByVal ParamArray movies As String())
    2.         Dim document As New Xml.XmlDocument()
    3.         Dim removeNode As Xml.XmlNode = Nothing
    4.         document.Load(dataFile)
    5.  
    6.         For Each movie As String In movies
    7.             removeNode = document.GetElementsByTagName("MovieName").OfType(Of Xml.XmlNode)().Where(Function(node) node.NamespaceURI.Contains(movie)).FirstOrDefault()
    8.             If removeNode IsNot Nothing Then
    9.                 removeNode.ParentNode.RemoveChild(removeNode)
    10.             End If
    11.         Next
    12.  
    13.         document.Save(dataFile)
    14.  
    15.     End Sub
    16.  
    17.     Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    18.         Me.RemoveMovies(Me.ListView2.Items.OfType(Of ListViewItem)().Select(Function(item) item.Text).ToArray())
    19.     End Sub

Tags for this Thread

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