|
-
Feb 1st, 2005, 07:34 PM
#1
Thread Starter
Hyperactive Member
Connection String to an XML file (Resolved)
I am working on an Art Flash Card program and need to use XML as my datasource. I'm pretty sure that I can use SQL statements to work with the data once I make the connection but I am not sure how to make the connection in the first place.
This is my XML File. It is named C:\FlashCards\FlashCardData.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<ArtWork>
<Art>
<ID>1</ID>
<title>Blue World</title>
<Artist>Steve Jackson</Artist>
<Path>c:\artwork\blu01.jpg</Path>
<Style>Abstract</Style>
</Art>
<Art>
<ID>2</ID>
<title>The Body</title>
<Artist>George Willard</Artist>
<Path>c:\artworkbo01.jpg</Path>
<Style>Modern</Style>
</Art>
<Art>
<ID>3</ID>
<title>Flashlight</title>
<Artist>Bob Miller</Artist>
<Path>c:\artqorkfl01.jpg</Path>
<Style>Modern</Style>
</Art>
</ArtWork>
I have never worked with XML as a datasource before but I think that is the correct format for doing what I need.
Last edited by BukHix; Feb 1st, 2005 at 11:12 PM.
-
Feb 1st, 2005, 08:31 PM
#2
Re: Connection String to an XML file
You don't have to make a connection as such, you can just load the XML directly into a DataSet, i.e.
VB Code:
Dim ds As New DataSet
Dim xml As String
xml = "<?xml version='1.0' encoding='utf-8' ?><ArtWork><Art><ID>1</ID><title>Blue World</title><Artist>Steve Jackson</Artist><Path>c:\artwork\blu01.jpg</Path><Style>Abstract</Style></Art><Art><ID>2</ID><title>The Body</title><Artist>George Willard</Artist><Path>c:\artworkbo01.jpg</Path><Style>Modern</Style></Art><Art><ID>3</ID><title>Flashlight</title><Artist>Bob Miller</Artist><Path>c:\artqorkfl01.jpg</Path><Style>Modern</Style></Art></ArtWork>"
ds.ReadXml(New IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(xml)))
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Art"
And if you have the XML in a file, just pass the file name to the ReadXML method instead, i.e.
VB Code:
Dim ds As New DataSet
ds.ReadXml("c:\data.xml")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Art"
-
Feb 1st, 2005, 08:51 PM
#3
Thread Starter
Hyperactive Member
Re: Connection String to an XML file
Thanks. Is it possible to do something like this now.
Code:
cmd.CommandText = "Select ID, Title, Artist, " & _
"Path, Style From TableName"
Is it possible to do that with an XML File?
-
Feb 1st, 2005, 09:06 PM
#4
Re: Connection String to an XML file
You can't use the Server Connectivity classes, i.e. DataAdapter, Connection, Command, etc..
But you don't need to, you have the data loaded into a DataSet, which lays it out into Tables and Fields.
You can access "SELECT" functionality via the DataTable instance, i.e.
VB Code:
' Return all entries by Artist "Steve Jackson"
Dim table As DataTable = ds.Tables("Art")
Dim rows() As DataRow = table.Select("Artist = 'Steve Jackson'")
Dim row As DataRow
For Each row In rows
Console.WriteLine(row("Title").ToString())
Next
-
Feb 1st, 2005, 09:55 PM
#5
Thread Starter
Hyperactive Member
Re: Connection String to an XML file
Thanks Aaron. That worked perfectly. I have one more question though. How can I make the selection random? Say for instance I have 50 records in my XML file and I want to generate a random record every time the button is pressed.
This is what I have so far but it randomizes from 0 to 10. How do I make it pick numbers between 1 - 50?
Code:
Dim intR As Integer
intR = (Rnd() * 10)
MsgBox(intR)
Dim ds As New DataSet
ds.ReadXml _
("C:\Visual Studio Projects\FlashCards\FlashCardData.xml")
Dim table As DataTable = ds.Tables("Art")
Dim rows() As DataRow = table.Select("ID = '" & intR & "'")
Dim row As DataRow
For Each row In rows
MsgBox(row("Artist"))
Next
-
Feb 1st, 2005, 11:10 PM
#6
Thread Starter
Hyperactive Member
Re: Connection String to an XML file
I got it now.
Code:
Dim oRnd As New Random(CInt(Now.Ticks And Integer.MaxValue))
Dim iValue As Int32 = oRnd.Next(1, 51)
Dim intR As Integer = iValue.ToString
Dim ds As New DataSet
ds.ReadXml _
("C:\Visual Studio Projects\FlashCards\FlashCardData.xml")
Dim table As DataTable = ds.Tables("Art")
Dim rows() As DataRow = table.Select("ID = '" & intR & "'")
Dim row As DataRow
For Each row In rows
pbArtImage.Image = Image.FromFile(row("Path"))
lblArtist.Text = (row("Artist"))
lblTitle.Text = (row("Title"))
Next
Thanks again!
Last edited by BukHix; Feb 1st, 2005 at 11:13 PM.
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
|