Create Table From XML

Mar 14, 2011 at 3:55 PM

Hello,

I've problems creating tables from XML files.

I created a table by code (Type: Person).

    Public Sub WP7DB_Create()
        'Create DB
        If Not Database.DoesDatabaseExists("WP7DB") Then
            db = Database.CreateDatabase("WP7DB")
            db.CreateTable(Of Person)()
            db.Save()
        Else
            Database.DeleteDatabase("WP7DB")
            db = Database.CreateDatabase("WP7DB")
            db.CreateTable(Of Person)()
            db.Save()
        End If

        'Add Row
        db.Table(Of Person)().Add(NewPerson("John", "Mayer"))
        db.Save()
    End Sub

Then I tried to create a table from XML (Type: Person).

 Private Sub CreateTableFromXML(ByVal FileName As String)
        If db IsNot Nothing Then
            Dim content As String = String.Empty
            Dim uri As New Uri(FileName, UriKind.Relative)
            Dim info As StreamResourceInfo = Application.GetResourceStream(uri)
            Using reader As New StreamReader(info.Stream)
                content = reader.ReadToEnd()
            End Using
            db.CreateNewTable(content, GetType(Person))
            db.Save()
        End If
    End Sub
XML file content:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPerson>
<Person>
<FirstName>George</FirstName>
<LastName>Washington</LastName>
</Person>
<Person>
<FirstName>Abraham</FirstName>
<LastName>Lincoln</LastName>
</Person>
</ArrayOfPerson>
Value of Database.Tables.Count was 2.

Now I tried to receive the data of the tables. But I only get back data from the first table. How can I get data of the second table?

        Dim queryTable = (From theTableEntries In db.Table(Of Person)() Select theTableEntries)
        For i As Integer = 0 To queryTable.Count - 1
            ListBox1.Items.Add(queryTable(i).LastName)
        Next
Mar 14, 2011 at 8:56 PM

I believe the issue is that you are using VB.  In VB collections are one-based (http://msdn.microsoft.com/en-us/library/a1y8b3b3(v=vs.80).aspx).

You need to change  your code to loop from 1 to queryTable.Coount.  Even better, you can just bind the collection to your ListBox, which is far better solution.