Tuesday, October 17, 2006

Serializable object to a file


Simple steps, take a deep breath and follow this list carefully. Its not as hard as it may sound!

Use a formatter to serialize the object and write it to the System.IO.FileStream object.

formaters available:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.Runtime.Serialization.Formatters.Soap.SoapFormatter

example:

public static void BinarySerialization(ArrayList list){
--using (FileStream str = File.Create("stuff.bin")){
----BinaryFormatter bf = new BinaryFormatter();
----bf.Serialize(str, list);
--}
}


public static ArrayList BinaryDeserialization(){
--ArrayList stuff = null;
--using (FileStream str = File.OpenRead("stuff.bin")){
----BinaryFormatter bf = new BinaryFormatter();
----stuff = (ArrayList)bf.Deserialize(str);
--}
--return stuff ;
}

No comments: