Create Table From XML

Mar 14, 2011 at 2:55 PM


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 = Database.CreateDatabase("WP7DB")
            db.CreateTable(Of Person)()
        End If

        'Add Row
        db.Table(Of Person)().Add(NewPerson("John", "Mayer"))
    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))
        End If
    End Sub
XML file content:
<?xml version="1.0" encoding="utf-8"?>
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
Mar 14, 2011 at 7:56 PM

I believe the issue is that you are using VB.  In VB collections are one-based (

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.