Results 1 to 6 of 6

Thread: [RESOLVED] How to display XML file data into a textbox?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Resolved [RESOLVED] How to display XML file data into a textbox?

    Hey everyone,
    I am trying to display my records from my external XML file that is located on my desktop,
    What I am trying to do is say I have a XML file called animals.xml (the name of my table) then in each record it will be like cat, dog, lion etc.

    <animals>
    <animal>
    <type>Cat
    </type>
    </animal>

    <animal>
    <type>Dog
    </type>
    </animal>
    </animals>

    then I create a brand new project in visual studio/ new form, then add a textbox and a button called next.
    How do I show the word Cat inside the textbox and then when you click onto the next button it will show Dog?

    I have found a video on youtube

    that shows you how to do it with MS Access but I want to use XML.

    If anyone knows how,
    that would be really awesome.

    Thanks,
    Jon

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to display XML file data into a textbox?

    try this:

    Code:
    Public Class Form1
    
        Dim xml As XElement = XElement.Load("animals.xml")
    
        Dim eIndex As Integer = 0
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            outputXValue()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'next
            eIndex += 1
            outputXValue()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'previous
            eIndex -= 1
            outputXValue()
        End Sub
    
        Private Sub outputXValue()
            Try
                TextBox1.Text = xml...<animal>(eIndex)...<type>.Value
            Catch ex As NullReferenceException
                TextBox1.Text = If(eIndex > 0, "Reached end of file", "Reached start of file")
            End Try
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: How to display XML file data into a textbox?

    Thank you so much, this works

    I just had to add my file path in the Dim xml As XElement = XElement.Load("animals.xml")
    section to locate the file from my desktop.

    Thank you once again.

    Jon

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: [RESOLVED] How to display XML file data into a textbox?

    Hey Paul, or anyone reading this,

    I am now trying to display two records at the same time from one XML file.
    Here is my XML file

    <?xml version="1.0" encoding="utf-8" standalone="yes" ?>

    <vocab>

    <lesson1>
    <lesson>Lesson 1
    </lesson>
    <word>Arid
    </word>
    <meaning>Dry; Lacking sufficient water.
    </meaning>
    </lesson1>
    <lesson1>
    <lesson>Lesson 1
    </lesson>
    <word>Clout
    </word>
    <meaning>A blow with the fist.
    </meaning>
    </lesson1>
    <lesson2>
    <lesson>Lesson 2
    </lesson>
    <word>A word
    </word>
    <meaning>something.
    </meaning>
    </lesson2>
    <lesson2>
    <lesson>Lesson 2
    </lesson>
    <word>Dawdle
    </word>
    <meaning>Waste time.
    </meaning>
    </lesson2>

    </vocab>

    What I have is a combobox and inside it has either (Lesson 1) (Lesson 2) and (All Lessons)
    What I want to try and do now is be able to show all the data from the xml file if Combobox is set to All Lessons.

    Here is the code I have so far:

    Private Sub outputXValue()
    If ComboBox1.Text = ("Lesson 1") Then
    Try
    TextBox1.Text = xml...<lesson1>(eIndex)...<word>.Value
    TextBox2.Text = xml...<lesson1>(eIndex)...<meaning>.Value
    Catch ex As NullReferenceException
    TextBox1.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
    TextBox2.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
    End Try
    ElseIf ComboBox1.Text = ("Lesson 2") Then
    Try
    TextBox1.Text = xml...<lesson2>(eIndex)...<word>.Value
    TextBox2.Text = xml...<lesson2>(eIndex)...<meaning>.Value
    Catch ex As NullReferenceException
    TextBox1.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
    TextBox2.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
    End Try
    ElseIf ComboBox1.Text = ("All Lessons") Then
    Try
    TextBox1.Text = xml...<lesson1>...<lesson2>(eIndex)...<word>.Value
    TextBox2.Text = xml...<lesson1>...<lesson2>(eIndex)...<meaning>.Value
    Catch ex As NullReferenceException
    TextBox1.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
    TextBox2.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
    End Try
    End If
    End Sub

    When I select (Lesson 1 and Lesson 2) one by one it works perfectly.
    However I would like to know how do you combine them both to show all data using the same method as in this thread with the next buttons and previous buttons etc?

    Jonathan

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] How to display XML file data into a textbox?

    try this:

    Code:
    Private Sub outputXValue()
        If ComboBox1.Text = ("Lesson 1") Then
            Try
                TextBox1.Text = xml...<lesson1>(eIndex)...<word>.Value
                TextBox2.Text = xml...<lesson1>(eIndex)...<meaning>.Value
            Catch ex As NullReferenceException
                TextBox1.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
                TextBox2.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
            End Try
        ElseIf ComboBox1.Text = ("Lesson 2") Then
            Try
                TextBox1.Text = xml...<lesson2>(eIndex)...<word>.Value
                TextBox2.Text = xml...<lesson2>(eIndex)...<meaning>.Value
            Catch ex As NullReferenceException
                TextBox1.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
                TextBox2.Text = If(eIndex > 0, "End of lesson", "Start of Lesson")
            End Try
        ElseIf ComboBox1.Text = ("All Lessons") Then
            Try
                TextBox1.Text = xml...<lesson1>(eIndex)...<word>.Value
                TextBox2.Text = xml...<lesson1>(eIndex)...<meaning>.Value
            Catch ex As NullReferenceException
                Dim tempIndex As Integer = If(eIndex < 0, eIndex, eIndex - xml...<lesson1>.Count)
                Try
                    TextBox1.Text = xml...<lesson2>(tempIndex)...<word>.Value
                    TextBox2.Text = xml...<lesson2>(tempIndex)...<meaning>.Value
                Catch ex2 As NullReferenceException
                    TextBox1.Text = If(eIndex < 0, "Start of Lesson", "End of lesson")
                    TextBox2.Text = If(eIndex < 0, "Start of Lesson", "End of lesson")
                End Try
            End Try
        End If
    End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: [RESOLVED] How to display XML file data into a textbox?

    Hey Paul, that is the codes I wanted thank you so much it works!!!!!

    Jon

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