Help with saving data from data table to xml file using VB.NET

Hi,

I tried saving data from datatable dt to an xml file using the following code

dt.Writexml("c:\test123.xml")

but received error message:

Cannot realize the Data Table. DataTable is not set.

How do I solve this problem?
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArkCommented:
How do you fill datatable?
0
Fernando SotoRetiredCommented:
Hi vcharles;

In order to answer a question like this we need to see the code that created the data table and have you verified that the data table really has data.
0
vcharlesAuthor Commented:
Hi,

The table was created with the code below, is it possible to achieve directly from the datatable without using a dataset?

   Dim dt As New DataTable()
        dt.Columns.Add("Link_ID")
        dt.Columns.Add("NSN")
        dt.Columns.Add("NASC")
        dt.Columns.Add("Receiver")
       

        Dim linker As XElement = XElement.Load(Application.StartupPath + "\LinkFinal.xml")
        Dim receiver As XElement = XElement.Load(Application.StartupPath + "\Receiver.xml")
        Dim NSN As XElement = XElement.Load(Application.StartupPath + "\NSNAA.xml")
        Dim NASC As XElement = XElement.Load(Application.StartupPath + "\NASC.xml")
'

        For Each item As XElement In linker.Elements("Row")
            Dim linkID As String = item.Element("Link_ID").Value
            Dim receiverId As String = item.Element("Receiver_ID").Value
            Dim NSNID As String = item.Element("C_ID").Value 'Add (3)
            Dim NASCId As String = item.Element("NASC_ID").Value

Dim receiverVal As String = String.Empty
            Dim xe As XElement = receiver.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Receiver_ID").Value = receiverId).FirstOrDefault()
            If xe IsNot Nothing Then
                receiverVal = xe.Element("Receiver").Value
            End If

            Dim NSNVal As String = String.Empty
            xe = NSN.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("C_ID").Value = NSNID).FirstOrDefault()
            If xe IsNot Nothing Then
                NSNVal = xe.Element("NSN").Value
            End If

            Dim NASCVal As String = String.Empty
            xe = NASC.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("NASC_ID").Value = NASCId).FirstOrDefault()
            If xe IsNot Nothing Then
                NASCVal = xe.Element("NASC").Value
            End If


            Dim dr As DataRow = dt.NewRow()
            dr("Link_ID") = linkID
            dr("NSN") = NSNVal
            dr("NASC") = NASCVal
            dr("Receiver") = receiverVal
            dt.Rows.Add(dr)
        Next
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Fernando SotoRetiredCommented:
Hi vcharles;

The location in code where you have this line of code :

dt.WriteXml("c:\test123.xml")

within the same scope as the code in your last post?

Also please post the exact exception message and inner exception message when the exception happens.
0
vcharlesAuthor Commented:
Hi,

Below is the complete code and error message.

Dim dt As New DataTable()
        dt.Columns.Add("Link_ID")
        dt.Columns.Add("NSN")
        dt.Columns.Add("NASC")
        dt.Columns.Add("Receiver")
       

        Dim linker As XElement = XElement.Load(Application.StartupPath + "\LinkFinal.xml")
        Dim receiver As XElement = XElement.Load(Application.StartupPath + "\Receiver.xml")
        Dim NSN As XElement = XElement.Load(Application.StartupPath + "\NSNAA.xml")
        Dim NASC As XElement = XElement.Load(Application.StartupPath + "\NASC.xml")
'

        For Each item As XElement In linker.Elements("Row")
            Dim linkID As String = item.Element("Link_ID").Value
            Dim receiverId As String = item.Element("Receiver_ID").Value
            Dim NSNID As String = item.Element("C_ID").Value 'Add (3)
            Dim NASCId As String = item.Element("NASC_ID").Value

Dim receiverVal As String = String.Empty
            Dim xe As XElement = receiver.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Receiver_ID").Value = receiverId).FirstOrDefault()
            If xe IsNot Nothing Then
                receiverVal = xe.Element("Receiver").Value
            End If

            Dim NSNVal As String = String.Empty
            xe = NSN.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("C_ID").Value = NSNID).FirstOrDefault()
            If xe IsNot Nothing Then
                NSNVal = xe.Element("NSN").Value
            End If

            Dim NASCVal As String = String.Empty
            xe = NASC.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("NASC_ID").Value = NASCId).FirstOrDefault()
            If xe IsNot Nothing Then
                NASCVal = xe.Element("NASC").Value
            End If


            Dim dr As DataRow = dt.NewRow()
            dr("Link_ID") = linkID
            dr("NSN") = NSNVal
            dr("NASC") = NASCVal
            dr("Receiver") = receiverVal
            dt.Rows.Add(dr)
        Next

dt.Writexml("c:\test123.xml")  ****Error line

error message: Cannot realize the Data Table. DataTable is not set.
0
Fernando SotoRetiredCommented:
Hi vcharles;

I am not sure if the error message you posted was translated from another language but see if this change gets rid of the error Message.

One thing that sticks out is that in order to serialize data rows in the data table to an XML file the table needs a TableName because it is used in the XML file. So I would change this line of code :

Dim dt As New DataTable()

' to this, changing MyTableName to a table name of your choice

Dim dt As New DataTable("MyTableName")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
It worked.

Thank You.

V.
0
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.