Subscribe

RSS Feed (xml)

Store a Serializable Object to a File

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. 
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.
Figure 2.1: Contents of the people.bin file.
 
Figure 2.2: Contents of the people.xml file.

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail