Re: XML: Search and Destroy
Actually you can use LINQ/lambdas to do this:
vb.net 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("C:\test\movies.txt") '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("C:\test\movies.txt") 'save the xml with the removed node
End If
Return removeNode
End Function
Public Sub DoSomething()
Me.RemoveMovie("Robin")
End Sub
Re: XML: Search and Destroy
Way to hide the fact you are downloading copyrighted material illegally off the internet.
Re: XML: Search and Destroy
Re: XML: Search and Destroy
Quote:
Originally Posted by
Cyb3rH4Xter
No off topic please -.-
Uh... speaking of which, did you try the code?
Re: XML: Search and Destroy
Quote:
Originally Posted by
Cyb3rH4Xter
No off topic please -.-
You are barking up the wrong tree buddy.
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.
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:
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".
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
Re: XML: Search and Destroy
Try it like this:
vb.net Code:
Public Sub RemoveMovies(ByVal ParamArray movies As String())
Dim document As New Xml.XmlDocument()
Dim removeNode As Xml.XmlNode = Nothing
document.Load(dataFile)
For Each movie As String In movies
removeNode = document.GetElementsByTagName("MovieName").OfType(Of Xml.XmlNode)().Where(Function(node) node.NamespaceURI.Contains(movie)).FirstOrDefault()
If removeNode IsNot Nothing Then
removeNode.ParentNode.RemoveChild(removeNode)
End If
Next
document.Save(dataFile)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Me.RemoveMovies(Me.ListView2.Items.OfType(Of ListViewItem)().Select(Function(item) item.Text).ToArray())
End Sub