Question XML file into datagridview

Joe1

Active member
Joined
Dec 4, 2012
Messages
37
Programming Experience
Beginner
Hi,

I am trying to view an XML file into a DataGridView using Visual Studio 2010 express. After looking around i have come across this code


Dim filePath As String

filePath =
"path of file"

dsPara.ReadXml(filePath)

With DataGridView1

.DataSource = dsPara

.DataMember =
"parameters"
End With

Can anyone see a problem with this? I get an error saying "parameters" cannot be created."

Cheers
 
DataMember there refers to a DataTable named "parameters" in that DataSet. If the DataSet doesn't have a table with that name there will be a problem.
You can specify a valid table name, or just set the DataSource to a table directly, for example using a numeric index of Tables collection; source = ds.Tables(0).
 
Without seeing your complete block of code I can't pinpoint it exactly, but it is likely one of two possible things:


  1. The DataSet is not being correctly populated. Set a break after you read the XML into the DataSet and check whether it has created any tables, including one named "parameters".
  2. The value that you are setting DataMember to is incorrect. Check your spelling against what is in the XML file.

If you still need more help, please post your full code block and XML content.

Good luck!
 
I have created a new project to get this part to work before introducing it into my full program

Imports
System.Windows.Forms


 

Public
Class XML


 


Dim dsPara As New DataSet("parametertable.xml")



Private Property source As DataTable


 


Private Sub ReadXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadXML.Click


 

 


Dim filePath As String

filePath =
"C:\Users\jos\documents\visual studio 2010\Projects\ReadingXML\ReadingXML\parametertable.xml"

dsPara.ReadXml(filePath)


With DataGridView1

.DataSource = dsPara


'.DataMember = "parameterstable"


'.CaptionText = .DataMember


 


End With

So this is everything thats in there

My XML file is in the form

<parametertable>
<parameter>
<descrption>
</despcription>
</parameter>
<parameter>
<descrption>
</despcription>
</parameter>
<parameter>
<descrption>
</despcription>
</parameter>
</parametertable>
 
First of all, the XML is not correct...the closing tag for the <descrption> opening tag doesn't match. Make sure the closing tag is </descrption>.

When read into the DataSet, this XML content will create a 'parameter' table, therefore you should set .DataMember = "parameter".
 
Back
Top