Howto specify which DataTables of a DataSet will be stored with WriteXml?

G

Gordian

Hello,

I would like to save a DataSet to xml-file.
But I only want to save some DataTables of the DataSet,
not all.

The DataSet.WriteXml(...) methods does not allow this.
Does anyone knows a workaround?

An additional requirement is, to store all DataTables in the same file.

Thanks
Gordian
 
G

Gordian

Hi Cor,

yes - you have a fine "workaround".
DataTable dt1 = ds.Tables["MyTable1"];
ds.Tables.Remove("MyTable1");
ds.WriteXML("topath");
ds.Tables.Add(dt1);

--------------------

I did a Copy(), Clear() and Merge() instead:

DataTable dt1 = ds.Tables["MyTable1"].Copy();
ds.Tables["MyTable1"].Clear();
ds.WriteXML("topath");
ds.Tables["MyTable1"].Merge(dt1);

But I think your way is much sprucer.
 
S

Sahil Malik [MVP]

Merge is not a good idea. A much more efficient way is to just remove the
DataTable, do a WriteXml and add it back in. Of course if you have many
DataRelations then it gets hairier than that.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------------------------

Gordian said:
Hi Cor,

yes - you have a fine "workaround".
DataTable dt1 = ds.Tables["MyTable1"];
ds.Tables.Remove("MyTable1");
ds.WriteXML("topath");
ds.Tables.Add(dt1);

--------------------

I did a Copy(), Clear() and Merge() instead:

DataTable dt1 = ds.Tables["MyTable1"].Copy();
ds.Tables["MyTable1"].Clear();
ds.WriteXML("topath");
ds.Tables["MyTable1"].Merge(dt1);

But I think your way is much sprucer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top