|
-
Oct 9th, 2009, 12:32 PM
#1
Thread Starter
Hyperactive Member
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.
-
Oct 9th, 2009, 12:48 PM
#2
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
-
Oct 9th, 2009, 12:52 PM
#3
Re: XML: Search and Destroy
Way to hide the fact you are downloading copyrighted material illegally off the internet.
-
Oct 9th, 2009, 01:03 PM
#4
Thread Starter
Hyperactive Member
Re: XML: Search and Destroy
-
Oct 9th, 2009, 01:06 PM
#5
Re: XML: Search and Destroy
 Originally Posted by Cyb3rH4Xter
No off topic please -.-
Uh... speaking of which, did you try the code?
-
Oct 9th, 2009, 01:09 PM
#6
Re: XML: Search and Destroy
 Originally Posted by Cyb3rH4Xter
No off topic please -.-
You are barking up the wrong tree buddy.
-
Oct 9th, 2009, 01:47 PM
#7
Thread Starter
Hyperactive Member
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.
-
Oct 10th, 2009, 01:42 PM
#8
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".
-
Oct 11th, 2009, 09:23 AM
#9
Thread Starter
Hyperactive Member
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
-
Oct 11th, 2009, 02:12 PM
#10
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|