How to load XML to control?

victor64

Well-known member
Joined
Nov 9, 2008
Messages
60
Programming Experience
Beginner
Hello,

How do you load a grid control with an XML file?, I tried the code below but can't figure out why it doesn't work.

Dim ds As New DataSet
ds.ReadXml("C:\aop29_XML\customer.xml")
C1TrueDBGrid1.DataSource = ds
C1TrueDBGrid1.Visible = True

Thanks,

Victor
 
A dataset can contain many tables and your grid control can only display a single table at a time. Therefore you must assign only a single table as your datasource.

DataGridView.DataSource = DataSetName.Tables("TableName")
 
The code below seems to work but the grid is blank, I tried using the DataSetName.Tables("Customer.xml") but it doesn't work. Any ideas on how to fix the code?

Thanks,

Victor

Code:

Dim myXMLfile As String = "C:\Documents and Settings\victor.m.charles\Desktop\aop29_XML\Customer.xml"
Dim ds As New DataSet()
' Create new FileStream with which to read the schema.
Dim fsReadXml As New System.IO.FileStream _
(myXMLfile, System.IO.FileMode.Open)

Try
ds.ReadXml(fsReadXml)
C1TrueDBGrid5.DataSource = ds ' change grid name here
C1TrueDBGrid5.DataMember = "Customer_ID" ' edit as necessary or comment out if only one table in DB

'Panel1.Visible = True
C1TrueDBGrid5.Visible = True
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
fsReadXml.Close()
End Try
 
Customer.xml is the name of your file not the name of the tables.

The below sample shows 0 being used which will load whatever is the first table in the dataset. Zero can be changed to include the table name once you figure out what they are...

VB.NET:
    Dim strFile as String = C:\Documents and Settings\victor.m.charles\Customer.xml"
    Dim dsCust as New DataSet

    dsCust.ReadXml(strFile)
    TrueDBGrid5.DataSource = dsCust.Tables(0)
 
I tried your suggestion but the Grid is still not pulling the data. I tried a listbox control and it's still not pulling the data, the listbox shows a few rows with the following data: System.Data.DataRowView.
 
Are you sure this is loading your xml file and the file itself contains records? Can you upload your form and the xml file so I can look it over and test it?
 

Latest posts

Back
Top