views:

419

answers:

4

Hi everyone,

I have a populated DataTable I'd like to serialize to a file for later use. I've been looking over the options involved with this and wondered if someone could point me in the right direction.

What I'll be creating are two methods-- one for writing the datatable to a file, and another for creating a new datatable using the file as input. Does it make sense to use the WriteXML() and Load() methods to do this, and if so, which flag(s) are ones to focus on? Thanks for the guidance.

I'm using .Net 2.0 if that helps.

+1  A: 

You might use the basic technique of serializing your database into CSV files with headers. Some database management systems support easy loading of data from such files. And in case your dbms doesn't it wouldn't be too difficult to write some code that'd do this for you. Does that answer your question?

In my opinion the disadvantage of xml is that it contains possibly more meta-data than actual data. In case of csv files meta-data is not repeated.

lewap
+2  A: 

I would go for the read/write xml methods. We use that pretty extensively. It's quick, it's easy, it's built into the framework.

Muad'Dib
A: 

Is the datatable an object in memory? If so, you could simply go with Serialize and Deserialize Methods. They are relatively quickly and you can persist the result anywhere you want.

Wagner Silveira
Which `Serialize` and `Deserialize` methods are you referring to?
John Saunders
You can use the methods within System.Xml.Serialization.XmlSerializer class. I'm at a client now, but when I have some time, I will try to get the examples I have. They are quite simple to use.
Wagner Silveira
Hi,Yes, the DataTable is in memory. I'd be interested in seeing you code snippet, as right now I'm using ReadXml() and WriteXml(). Thanks!
larryq
+1  A: 

I think Silveira comment mean use of binary serialization. And its right that it very fast compare to XML which serialization is very slow compare to binary specially for large amount of data. Also it take a lot less space on disk compare to XML.

    public static void Serialize(DataSet ds, Stream stream) {
        BinaryFormatter serializer = new BinaryFormatter();
        serializer.Serialize(stream, ds);
    }

    public static DataSet Deserialize(Stream stream) {
        BinaryFormatter serializer = new BinaryFormatter();
        return (DataSet)serializer.Deserialize(stream);
    } 
affan
One of shortfall in using binary serialization is that the binary serialization contain assembly qualified name of type been serialized so if System.Data.dll changes you will unable to read the data from binary file. But since DataSet and other classes in name space is part of framework and not like to change therefore i donot see any problem in this case.
affan
I usually had only to serialize small amounts of data, so XMLSerializer was fine. But the bottom line here is that we all seem to agree that Serialization is a good way forward. The right formatter (XML, Binary, DataContract), can be chosen depending on other requirements (e.g. if you only have to read the data within your app, and don't need to export it to other environment, Binary seems the perfect choice).
Wagner Silveira
Thanks for the tip, Affan
larryq