Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

FAIL READING XML INTO DATATABLE

Hi All,

I have a xml table and I want to load it into datatable, but failed to do so.

What's wrong ?

Thank you.
Imports System
Imports System.Data
Imports System.Data.SqlClient

Imports System.IO

Public Class frmReadXML

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try

            Dim strSourceFromClientFolder As String = System.AppDomain.CurrentDomain.BaseDirectory()
            Dim strXMLFileName As String = "121110110334.xml"

            ' Read in XML from file
            Dim dtTable As DataTable = Me.Read_XML_To_DataTable(strSourceFromClientFolder, strXMLFileName)

            ' Bind DataSet to Data Grid
            dgvData.DataMember = "Data"
            dgvData.DataSource = dtTable

        Catch ex As Exception
            MsgBox("Gagal Buka XML File ...!", MsgBoxStyle.Information, Me.Text)
        End Try

    End Sub

    Private Function Read_XML_To_DataTable(ByVal strFolderFile As String, _
                                                 ByVal strXMLFileName As String) As DataTable

        Try

            Dim dsTable As New DataSet
            Dim fs As FileStream

            fs = New FileStream(strFolderFile & strXMLFileName, FileMode.Open)

            dsTable.ReadXml(fs)

            Return dsTable.Tables(0)

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

        Return Nothing

    End Function

Open in new window

121110110334.xml
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

>> I have a xml table and I want to load it into datatable, but failed to do so.

Do you have any error ? I just tried it here, and it loads into several tables...
Avatar of emi_sastra
emi_sastra

ASKER

Hi Dhaest,

Please see the result.

Thank you.
READ-XML.PNG
How could load those tables to datagridview?

Thank you.
That's very hard, because you have a multilevel xml-file. It will be split into several datatables.

What would you like to see in your datagrid ?
From where does your xml come from ? Is it coming from a database, why don't you query the database ?

- That's very hard, because you have a multilevel xml-file. It will be split into several datatables.
Could we use 2 datagridview, one for tables and one for its table data.

- What would you like to see in your datagrid ?
The data there. We want to construct it.

- From where does your xml come from ? Is it coming from a database, why don't you query the database ?
It is exported from firebird, we can not direct access to it, since the vendor not allowed it.

Thank you.
>>  Could we use 2 datagridview, one for tables and one for its table data.

Yes you can fe load all the tablenames into a combobox and depending of what's selected, show this into the datagrid. But it all has keys (foreign keys), so I'm afraid that it's not very usefull information (in some tables)
-Yes you can fe load all the tablenames into a combobox and depending of what's selected, show this into the datagrid. But it all has keys (foreign keys), so I'm afraid that it's not very usefull information (in some tables)
Never mind. If it is the case, I just curious how to load it.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for the code above.

What I am struggling now is how to load the xml file to dataset.

Thank you.
Hi Dhaest,

Any solution ?

Thank you.
Hi Dhaest,

I have succeed read xml to dataset and field dataset table names to datagridview (dgvTables).

How I could load data to datagridview (dgvData) when user click dgvTables using the xml data ?

Thank you.
         

dsTables.ReadXml(strXMLFileName)

            dgvTables.Columns.Add("Table", "Table")

            For Each tbl As DataTable In dsTables.Tables
                Dim dgvRow As New DataGridViewRow
                Dim dgvCell As DataGridViewCell

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = tbl.TableName
                dgvRow.Cells.Add(dgvCell)

                dgvTables.Rows.Add(dgvRow)
            Next

Open in new window