-
Jan 29th, 2018, 04:45 AM
#1
Thread Starter
New Member
[RESOLVED] Can't read xml data to DataTable
I got a problem in putting xml file data into DataTable. Here's my code:
Code:
Public Class input
Dim ndt As New DataTable("saved")
Private Sub load_Click(sender As Object, e As EventArgs) Handles load.Click
Dim result As Integer = MessageBox.Show("Confirm to load saved data and clear existing one?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If result = DialogResult.Yes Then
ndt.Columns.Add("bar_cd")
ndt.ReadXml("C:\report\stock_take.xml")
End If
DataGridView1.DataSource = ndt
End Sub
End Class
The xml file contains data in it. I tried couple method and still doesn't work. For example:
Code:
If result = DialogResult.Yes Then
Try
ndt.ReadXml("C:\report\stock_take.xml")
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
End If
DataGridView1.DataSource = ndt
What's the problem?
-
Jan 29th, 2018, 04:50 AM
#2
Re: Can't read xml data to DataTable
The problem is that you didn't provide an example of the XML that fails or tell us what the error message is when it fails.
-
Jan 29th, 2018, 05:00 AM
#3
Thread Starter
New Member
Re: Can't read xml data to DataTable
Sorry about that, and sadly no error message was shown.
My xml file contains only 1 integer.
Code:
<NewDataSet>
<barcd>
<bar_cd>124557</bar_cd>
</barcd>
</NewDataSet>
That's it. And when I debug my program, the DataTable contains nothing after read.xml(path) and IDE shows no error.
-
Jan 29th, 2018, 06:04 AM
#4
Re: Can't read xml data to DataTable
Have you debugged? Did execution enter the Catch block?
-
Jan 29th, 2018, 10:56 AM
#5
Re: Can't read xml data to DataTable
Hi,
We encountered this issue before using DataTable.ReadXml() wherein the solution was to create a schema file (.xsd) based from the Xml file and in the VB code, read the schema file first using ReadXmlSchema() followed by ReadXml().
VB.NET Code:
ndt.ReadXmlSchema(xsdPath) ndt.ReadXml(xmlPath) DataGridView1.DataSource = ndt
Another alternative is to use DataSet.ReadXml() which does not require creating schemas and stuff.
- kgc
Last edited by KGComputers; Jan 29th, 2018 at 11:00 AM.
-
Jan 29th, 2018, 10:58 AM
#6
Re: Can't read xml data to DataTable
comment this out
Code:
ndt.Columns.Add("bar_cd")
or adding it after the readxml
Last edited by kpmc; Jan 29th, 2018 at 11:11 AM.
-
Jan 29th, 2018, 11:00 AM
#7
Re: Can't read xml data to DataTable
Originally Posted by KGComputers
Hi,
We encountered this issue before using DataTable.ReadXml() wherein the solution was to create a schema file (.xsd) based from the Xml file and in the VB code, read the schema file first using ReadXmlSchema() followed by ReadXml().
VB.NET Code:
ndt.ReadXmlSchema(xslLocationSchema) ndt.ReadXml(xslLocation) DataGridView1.DataSource = ndt
Another alternative is to use DataSet.ReadXml() which does not require creating schemas and stuff.
- kgc
on this note you could also try to infer the schema
Code:
DSet.ReadXml("yourxml", XmlReadMode.InferSchema)
-
Jan 29th, 2018, 02:35 PM
#8
Re: Can't read xml data to DataTable
Here is an example where I create classes from the XML and use those to populate the data.
http://www.vbforums.com/showthread.p...=1#post5252773
-
Jan 30th, 2018, 10:04 PM
#9
Thread Starter
New Member
Re: Can't read xml data to DataTable
Thanks for all replying, and now i can read the data using .ReadXmlSchema method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|