Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27348

VS 2010 Reading XML and Populating TextBoxes PROBLEM!

$
0
0
Hello guys!

What I'm trying to do is read an xml file, and populate the textboxes in my form with the data in the xml file. What happens is that everytime, i want a different XML file, and to do so I need to change the id, like this

file.xml?id=300
file.xml?id=301

I have managed to download the xml file to my computer and get it read, but when I try to download onother one by changing the ID it says I can't download it because the current one is being used by a process and it can't be overwritten. I have tried every way to kill the process and delete the file beforehand, but it just won't work.

This is my code:

Code:

Imports System
Imports System.Xml

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'check if file myxml.xml is existing
        If (IO.File.Exists("C:\Users\Aristide\Desktop\xmlPractice\movie.xml")) Then

            'create a new xmltextreader object
            'this is the object that we will loop and will be used to read the xml file
            Dim document As XmlReader = New XmlTextReader("C:\Users\Aristide\Desktop\xmlPractice\movie.xml")

            'loop through the xml file
            While (document.Read())

                Dim type = document.NodeType

                'if node type was element
                If (type = XmlNodeType.Element) Then

                    'if the loop found a <FirstName> tag
                    If (document.Name = "MovieTitleClean") Then

                        TextBox1.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <LastName tag
                    If (document.Name = "Genre1") Then

                        TextBox2.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <FirstName> tag
                    If (document.Name = "MovieYear") Then

                        TextBox3.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <FirstName> tag
                    If (document.Name = "LargeCover") Then

                        TextBox4.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <FirstName> tag
                    If (document.Name = "TorrentUrl") Then

                        TextBox5.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <FirstName> tag
                    If (document.Name = "ShortDescription") Then

                        TextBox6.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <FirstName> tag
                    If (document.Name = "MovieRating") Then

                        TextBox8.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <FirstName> tag
                    If (document.Name = "MovieRuntime") Then

                        TextBox9.Text = document.ReadInnerXml.ToString()

                    End If

                    'if the loop found a <FirstName> tag
                    If (document.Name = "YoutubeTrailerUrl") Then

                        TextBox10.Text = document.ReadInnerXml.ToString()

                    End If

                End If

            End While

        Else

            MessageBox.Show("The filename you selected was not found.")
        End If

    End Sub
    Public Sub ClearTextBox(ByVal root As Control)
        For Each ctrl As Control In root.Controls
            ClearTextBox(ctrl)
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = String.Empty
            End If
        Next ctrl
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim path As String = "http://yify-torrents.com/api/movie.xml?id=" + TextBox7.Text
        TextBox11.Text = path
        If IO.File.Exists("C:\Users\Aristide\Desktop\xmlPractice\movie.xml") = False Then

            My.Computer.Network.DownloadFile(
    "http://yify-torrents.com/api/movie.xml?id=4213",
    "C:\Users\Aristide\Desktop\xmlPractice\movie.xml")

            MsgBox("XML file saved.")
        Else
            For Each P As Process In System.Diagnostics.Process.GetProcessesByName("vshost32")
                P.Kill()
            Next

            MsgBox("Error downloading")
            My.Computer.FileSystem.DeleteFile("C:\Users\Aristide\Desktop\xmlPractice\movie.xml")
            Button2.PerformClick()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        ClearTextBox(Me)
    End Sub
End Class

The red code is where I've tried to fix it but failed.
What can I do now?

Viewing all articles
Browse latest Browse all 27348

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>