Solved

READ MULTIPLE TABLES OF XML FILE TO DATASET

Posted on 2011-03-17
8
681 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:emi_sastra
  • 4
  • 4
8 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
Very weird.

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

dsTables.ReadXml(strXMLFileName)

Why is that ?

Thank you.


0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Heheh...   noob mistake on my part. Initialize the dataset:
Dim dsTables As New DataSet
Dim strXMLFileName As String = strSourceFromClientFolder & "121110110334.xml"

dsTables.ReadXml(strXMLFileName)

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
I've tried it.

The grid show nothing ?

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Awesome! Glad to help  = )
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now