How to use WriteXml() for a DataTable?

  • Thread starter Thread starter Dee
  • Start date Start date
D

Dee

Hi
I am reading in data from a file and storing in a DataTable variable.
I tried to use the DataSet.Write Xml() but got the following error:
---- >System.NullReferenceException: Object reference not set to an instance
of an object.

My code:

dt as New DataTabe()
' Fill dt.
dt.DataSet.WriteXml("c:\data.xml")

How can I get useWriteXml() ?

Thanks very much
Dee
 
Dee,

You can not write a datatable or you should first serialize it. A little bid
difficult when you can do all things in once by using a dataset.
dt as New DataTabe()
' Fill dt.
dt.DataSet.WriteXml("c:\data.xml")
dim ds as New Dataset
fill(ds)
ds.WriteXML("c:\data.xml")

When you would build the datatable yourself and not get it from the fill
than it can be.
dim ds as New Dataset
dim dt as New Datatable
ds.tables.add(dt)
ds.Wr.........................

I hope this helps,

Cor
 
Thanks Cor,
How can I fill a DataSet amnually, the way one does a DataTable?
So I could do:
dim ds as New Dataset
' Fill dataset by assigning fields for every row the way you do with a
datatable ??
ds.WriteXML("c:\data.xml")
Tanks again
Dee
 
Dee,

I an not sure if you understood it or not.

A dataset holds collections of datatables which hold collection of datarows,
which hold collection of items. The items are described by the collection of
datacolumns. The tables can be related too each other in the dataset using
the relations.

Cor
 
Thanks Cor

I did it this way:

ds as New DataSet()
dt as New DataTabe()
' Fill dt. row by row, item by item
' ...
dt = ds.Tables(0)
ds.WriteXml("data.xml")

Dee :)
 
Dee,

And that worked, I will be suprised because in my opinion I wrote
ds as New DataSet()
dt as New DataTabe()
' Fill dt. row by row, item by item
' ... ds.Tables.Add(dt)
ds.WriteXml("data.xml")

Cor
 
Back
Top