Results 1 to 9 of 9

Thread: [RESOLVED] Can't read xml data to DataTable

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    14

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    14

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Can't read xml data to DataTable

    Have you debugged? Did execution enter the Catch block?

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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:
    1. ndt.ReadXmlSchema(xsdPath)
    2. ndt.ReadXml(xmlPath)
    3. 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.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  6. #6
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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.

  7. #7
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Can't read xml data to DataTable

    Quote Originally Posted by KGComputers View Post
    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:
    1. ndt.ReadXmlSchema(xslLocationSchema)
    2. ndt.ReadXml(xslLocation)
    3. 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)

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    14

    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
  •  



Click Here to Expand Forum to Full Width