2.12 Store a Serializable Object to a File


Problem

You need to store a serializable object and its state to a file and then deserialize it later.

Solution

Use a formatter to serialize the object and write it to a System.IO.FileStream . When you need to retrieve the object, use the same type of formatter to read the serialized data from the file and deserialize the object. The .NET Framework class library includes the following formatter implementations for serializing objects to binary or SOAP format:

  • System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

  • System.Runtime.Serialization.Formatters.Soap.SoapFormatter

Discussion

Using the BinaryFormatter and SoapFormatter classes, you can serialize an instance of any type that's decorated with the attribute System.SerializableAttribute . The BinaryFormatter produces a binary data stream representing the object and its state, whereas the SoapFormatter produces a SOAP document.

Both the BinaryFormatter and SoapFormatter classes implement the interface System.Runtime.Serialization.IFormatter , which defines two methods : Serialize and Deserialize . The Serialize method takes a System.IO.Stream reference and a System.Object reference as arguments, serializes the Object , and writes it to the Stream . The Deserialize method takes a Stream reference as an argument, reads the serialized object data from the Stream , and returns an Object reference to a deserialized object. You must cast the returned Object reference to the correct type.

Important  

To call the Serialize and Deserialize methods of the BinaryFormatter class, your code must be granted the SerializationFormatter element of the permission System.Security.Permissions.SecurityPermission .

To call the Serialize and Deserialize methods of the SoapFormatter class, your code must be granted full trust because the System.Runtime.Serialization.Formatters.Soap.dll assembly in which the SoapFormatter class is declared does not allow partially trusted callers . Refer to recipe 13.1 for more information about assemblies and partially trusted callers .

The BinarySerializationExample class listed here demonstrates the use of a BinaryFormatter to serialize a System.Collections.ArrayList containing a list of people to a file. The ArrayList is then deserialized from the file and the contents displayed to the console.

 using System.IO; using System.Collections; using System.Runtime.Serialization.Formatters.Binary; public class BinarySerializationExample {     public static void Main() {                  // Create and configure the ArrayList to serialize         ArrayList people = new ArrayList();         people.Add("Graeme");         people.Add("Lin");                             people.Add("Andy");         // Serialize the ArrayList object         FileStream str = File.Create("people.bin");         BinaryFormatter bf = new BinaryFormatter();         bf.Serialize(str, people);         str.Close();                  // Deserialize the ArrayList object         str = File.OpenRead("people.bin");         bf = new BinaryFormatter();         people = (ArrayList)bf.Deserialize(str);         str.Close();                  // Display the contents of the deserialized ArrayList object         foreach (string s in people) {                          System.Console.WriteLine(s);         }     }    } 

You can use a SoapFormatter class in exactly the same way as shown in the BinarySerializationExample class; all you need to do is replace each instance of BinaryFormatter with SoapFormatter and change the using directives to import the System.Runtime.Serialization.Formatters.Soap namespace. You must also include a reference to the System.Runtime.Serialization.Formatters.Soap.dll assembly when you compile the code. The file SoapSerializationExample.cs in the sample code for this chapter contains an example of how to use the SoapFormatter class.

To illustrate the different results achieved using the BinaryFormatter and SoapFormatter classes, Figure 2.1 shows the contents of the people.bin file generated using the BinaryFormatter class, whereas Figure 2.2 shows the contents of the people.xml file generated using the SoapFormatter class.

click to expand
Figure 2.1: Contents of the people.bin file.
click to expand
Figure 2.2: Contents of the people.xml file.



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net