Friday, October 01, 2004

Serializing DataTable Objects

A MUST READ Dino Esposito's powerfull article on Binary Serialization of ADO.NET Objects
An excerpt from it :

Serializing DataTable ObjectsAlone in the System.Data namespace, the DataSet object supplies a series of methods to serialize and deserialize itself in a class-defined XML format. I personally call this format the ADO.NET XML normal form. The WriteXml method of the DataSet class saves the contents of all the child tables and relations to various output streams, including disk files and text writers.The DataTable class doesn't provide the same WriteXml method to persist its contents to XML. This means that if you have a standalone DataTable object, which is an object that's not included as a child in any parent DataSet object, you must resort to a trick to have it persisted as XML.When you persist a DataSet object to XML, any contained DataTable object is rendered as XML, but the necessary methods are not publicly available. To work around the issue, you simply create a temporary, empty DataSet object, add the table to it, and then serialize the DataSet object to XML. The code in Figure 1 demonstrates a static method that takes a DataTable object and serializes it to a disk file using the same set of writing modes supported by DataSet objects. The WriteDataTable method has several overloads and mimics the DataSet object's WriteXml method as much as possible. In Figure 1, the input DataTable object is incorporated in a temporary DataSet named DataTable. Of course, in your own implementation you can change this name or, better yet, make it configurable by the user. Note that the name you use slightly influences the resulting XML output. The name of the DataSet, in fact, represents the root node of the final XML document:
DataSet ds = new DataSet("DataTable");
if (dt.DataSet == null)
ds.Tables.Add(dt);
else ds.Tables.Add(dt.Copy());
Also note that a DataTable object can't be linked to more than one DataSet object at a time. If a DataTable object belongs to a parent object, then its DataSet property is not null. In this case, the temporary DataSet object used to serialize the table must be linked to an in-memory copy of the table. The Copy method just creates a perfect clone of the specified DataTable object. If you prefer, you can implement the ISerializable interface to serialize the DataTable object (see the sidebar "Implementing ISerializable").The class library that contains the WriteDataTable overloads is available with this month's source code at the link at the top of this article and is named MsdnMagAdoNetSerializer. A client application makes use of the library as follows: StringWriter writer = new StringWriter();
MsdnMagAdoNetSerializer.WriteDataTable(table, writer);
// Show the serialization output
OutputText.Text = writer.ToString();
writer.Close();
In this code snippet, the WriteDataTable method writes its output as a string. The StringWriter class accepts data through the text writer interface, then exposes it as a string via ToString.

This page is powered by Blogger. Isn't yours?

© Copyright 2005, Rohan Thomas
Disclaimer
This Blog contains findings that are self-explored and expressions that are self-evoked. They are by no meanss representions of my employer's view.