-
Nov 15th, 2015, 03:07 PM
#1
Thread Starter
Member
[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
-
Nov 15th, 2015, 03:46 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 15th, 2015, 04:23 PM
#3
Thread Starter
Member
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
-
Nov 16th, 2015, 05:20 PM
#4
Thread Starter
Member
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
-
Nov 17th, 2015, 02:36 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 17th, 2015, 02:54 PM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|