DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.5 Deserializing Data

Problem

You have a DataSet that has been serialized and written to a file. You want to recreate the DataSet from this file.

Solution

Use the serializer object's Deserialize( ) method and cast the result as a DataSet.

The sample code loads a file stream containing a previously serialized DataSet in a specified format and deserializes it to recreate the original DataSet.

The C# code is shown in Example 5-5.

Example 5-5. File: DeserializeForm.cs
// Namespaces, variables, and constants
using System;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;

private OpenFileDialog ofd;

//  . . . 

private void goButton_Click(object sender, System.EventArgs e)
{
    // Create and open the stream for deserializing.
    Stream stream = null;
    try
    {
        stream = File.Open(fileNameTextBox.Text, FileMode.Open,
            FileAccess.Read);
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message, "Deserializing Data",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }

    // Deserialize the DataSet from the stream.
    DataSet ds = null;
    try
    {
        if (xmlReadRadioButton.Checked)
        {
            ds = new DataSet( );
            ds.ReadXml(stream);
        }
        else if (xmlSerializerRadioButton.Checked)
        {
            XmlSerializer xs = new XmlSerializer(typeof(DataSet));
            ds = (DataSet)xs.Deserialize(stream);
        }
        else if(soapRadioButton.Checked)
        {
            SoapFormatter sf = new SoapFormatter( );
            ds = (DataSet)sf.Deserialize(stream);
        }        
        else if(binaryRadioButton.Checked)
        {
            BinaryFormatter bf = new BinaryFormatter( );
            ds = (DataSet)bf.Deserialize(stream);
        }
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message, "Deserializing Data",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    finally
    {
        stream.Close( );
    }

    // Bind the DataSet to the grid.
    dataGrid.DataSource = ds.DefaultViewManager;

    MessageBox.Show("Deserialization complete.", "Deserializing Data",
        MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void fileDialogButton_Click(object sender, System.EventArgs e)
{
    // File dialog to save file
    if(xmlReadRadioButton.Checked || xmlSerializerRadioButton.Checked)
        ofd.Filter = "XML files (*.xml)|*.xml";
    else if(soapRadioButton.Checked)
        ofd.Filter = "SOAP files (*.soap)|*.soap";
    else if(binaryRadioButton.Checked)
        ofd.Filter = "Binary files (*.bin)|*.bin";

    ofd.Filter += "|All files (*.*)|*.*";
    ofd.FilterIndex = 0;

    if (ofd.ShowDialog( ) == DialogResult.OK)
        fileNameTextBox.Text = ofd.FileName;
}

Discussion

This sample deserializes any of the serialized DataSet objects from Recipe 5.4.

The sample allows the user to select a file and specify a serialization type. The appropriate serializing object is created and in the case of the XmlSerializer object, its type is specified in the constructor. The Deserialize( ) method of the serializer object is then used to deserialize the file stream into an object graph. This is then cast to a DataSet to complete the deserialization.

See the discussion in Recipe 5.4 for more information about the serialization and the formatter classes that can serialize ADO.NET objects.

    [ Team LiB ] Previous Section Next Section