Data serialization for later reuse

I have a populated DataTable that I would like to serialize to a file for later use. I looked through the options related to this and wondered if anyone could point me in the right direction.

What I will create are two methods: one for writing data to a file, and the other for creating a new data type using the file as input. Does it make sense to use the WriteXML () and Load () methods for this, and if so, which flag are those you need to focus on? Thanks for the guide.

I am using .Net 2.0 if this helps.

+7
c # datatable xml-serialization
source share
5 answers

I would go for reading / writing xml methods. We use this quite widely. It is fast, easy, it is built into the framework.

+10
source share

I think Silveira comment means using binary serialization. And it’s right that it compares very quickly with XML, whose serialization is very slow compared to binary ones especially for large amounts of data. In addition, it takes up much less disk space compared 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); } 
+13
source share

IMPORTANT POINT:
If you try to serialize a DataTable or a DataSet using binary formatting, you will still get the binary, but it's quite large because it is filled with tons of XML data. Unfortunately, the XML data in binary files does for huge files that do not have the portability and readability benefits provided by XML. Subsequently, deserializing such files may take several seconds and ultimately take up much more memory than is really necessary. As a result, if you choose binary serialization of ADO.NET objects because you need to get more compact output, you cannot . Binary serialization is still the most space-efficient, but with ADO.NET objects, it is not as efficient as it should be.

For full reference, read the following article: -
http://msdn.microsoft.com/en-us/magazine/cc188907.aspx

+7
source share

You can use the basic method of serializing your database in CSV files with headers. Some database management systems support the easy loading of data from such files. And in case your dbms do not do this, it would not be too difficult to write code that will do it for you. Does this answer your question?

In my opinion, the disadvantage of xml is that it contains perhaps more metadata than the actual data. In the case of csv files, metadata is not repeated.

+1
source share

Is a datatable object in memory? If so, you can just go with the Serialize and Deserialize methods. They are relatively fast, and you can save the result anywhere.

0
source share

All Articles