Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

READ MULTIPLE TABLES OF XML FILE TO DATASET

Hi All,

I have a xml file. It could be opened using browser, but I can not read it to dataset.

Please see my code below.

What's wrong ?

Thank you.
Dim dsTables As DataSet = Nothing
        Dim xmlFile As XmlReader

        Try

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

            xmlFile = XmlReader.Create(strXMLFileName, New XmlReaderSettings())

            dsTables.ReadXml(xmlFile)

            dgvTables.DataSource = dsTables

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

Open in new window

121110110334.xml
Avatar of kaufmed
kaufmed
Flag of United States of America image

Works fine for me with just this:
Dim dsTables As DataSet = Nothing
Dim strXMLFileName As String = strSourceFromClientFolder & "121110110334.xml"

dsTables.ReadXml(strXMLFileName)

Open in new window

Avatar of emi_sastra
emi_sastra

ASKER

Very weird.

I've got exception "object reference not set to an instance of object" at :

dsTables.ReadXml(strXMLFileName)

Why is that ?

Thank you.


ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America 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
Yes, it works.

Would you please help how to fill it to fill to datagridview ?


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

                dgvCell = New DataGridViewTextBoxCell()
            ------->    dgvCell.Value = dtTable.n
                dgvRow.Cells.Add(dgvCell)
 
                dgvTables.Rows.Add(dgvRow)
            Next

Thank you.
It should be like what you had above:
Dim dsTables As New DataSet
Dim strXMLFileName As String = strSourceFromClientFolder & "121110110334.xml"

dsTables.ReadXml(strXMLFileName)
dgvTables.DataSource = dsTables

Open in new window

I've tried it.

The grid show nothing ?

Thank you.
I've got it.

 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


Thank you very much for your help.
Awesome! Glad to help  = )