Solved

READ MULTIPLE TABLES OF XML FILE TO DATASET

Posted on 2011-03-17
8
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35156201
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
ID: 35156482
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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35156585
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 1

Author Comment

by:emi_sastra
ID: 35156690
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35156774
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
ID: 35156852
I've tried it.

The grid show nothing ?

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35157048
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 75

Expert Comment

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

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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