PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Can't read xml data to DataTable-VBForums
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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    Re: Can't read xml data to DataTable

    Have you debugged? Did execution enter the Catch block?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    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 | GitHub
    ___________________________________________________________________________________

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

  6. #6
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    465

    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
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    465

    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
    Pointless Forest 38.517,-92.023
    Posts
    9,034

    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
  •  



Featured


Click Here to Expand Forum to Full Width