Results 1 to 6 of 6

Thread: Connection String to an XML file (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    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.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    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:
    1. Dim ds As New DataSet
    2.       Dim xml As String
    3.  
    4.       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>"
    5.       ds.ReadXml(New IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(xml)))
    6.  
    7.       DataGrid1.DataSource = ds
    8.       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:
    1. Dim ds As New DataSet
    2.  
    3.       ds.ReadXml("c:\data.xml")
    4.  
    5.       DataGrid1.DataSource = ds
    6.       DataGrid1.DataMember = "Art"

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    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?

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    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:
    1. ' Return all entries by Artist "Steve Jackson"
    2. Dim table As DataTable = ds.Tables("Art")
    3. Dim rows() As DataRow = table.Select("Artist = 'Steve Jackson'")
    4. Dim row As DataRow
    5.  
    6. For Each row In rows
    7.    Console.WriteLine(row("Title").ToString())
    8. Next

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    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
  •  



Click Here to Expand Forum to Full Width