Serialization is a process of storing information to a storage medium; and retrieving this information back from the storage medium is called Deserialization. If you are already aware of the term Serialization then this should sound interesting to you. In Spread, you can serialize objects in the SpreadSheet or the entire SpreadSheet component using several methods. It is important to note the difference between the methods in FarPoint.Win.Serializer as opposed to the ones available in the model namespace FarPoint.Win.Spread.Model.SpreadSerializer. The SpreadSerializer is intended for saving and loading entire Spread component objects or text files from a sheet, while the Serializer contains methods for saving and loading any serializable object using our XML serialization implementation. You can serialize your data and deserialize it at some later stage. This is very useful if you want to transfer data from one sheet to another or if want to load data in another Spread in the same application. Spread supports two types of serializations i.e. XML Serialization and Binary Serialization.
XML Serialization
I would first write about XML Serialization with Spread** which is very easy and simple. Spread for Winforms has some methods to serialize and deserialize data through XML. These methods are FarPoint.Win.Serializer.GetObjectXML() and FarPoint.Win.Serializer.SetObjectXML(). GetObjectXML() is used to extract Spread data in to an XML string which can be deserialized and loaded to another SpreadSheet using SetObjectXML(). Following code snippets shows the implementation to serialize and deserialize Spread** data using XML serialization :
XMLString = FarPoint.Win.Serializer.GetObjectXml((FarPoint.Win.ISerializeSupport)fpSpread1.Sheets[0].Models.Data, "Data");
FarPoint.Win.Serializer.SetObjectXml((FarPoint.Win.ISerializeSupport)fpSpread1.Sheets[0].Models.Data, XMLString, "Data");
Spread Serializer class has two more methods to support XML serialization. These are SaveObject() and LoadObject(). SaveObject() saves an object as XML to a file or a stream and LoadObject() loads this object back to Spread.
FarPoint.Win.Serializer.SaveObject(typeof(FarPoint.Win.Spread.SheetView), "C:\\\SavedSheet.xml", "RootNode"); //serializing
FpSpread.Sheets.Add(FarPoint.Win.Serializer.LoadObject(typeof(FarPoint.Win.Spread.SheetView), "C:\\\SavedSheet.xml", "RootNode")) //deserializing
Binary Serialization
Now lets move to Binary Serialization. Binary serialization is done with the help of BinaryFormatter class present in .Net framework itself. BinaryFormatter can only serialize those classes/objects which are marked with the Serializable attribute or which implement the ISerializable interface. BinaryFormatter will persist the type information of an object being serialized inside the byte stream along with the type information of the sub-objects contained within the object being serialized. Spread is a serializable object as it implements the ISerializable interface. So are the CellTypes and Formulas in Spread. But you have to explicitly mark a custom class/function as serializable in order to serialize it. Here is an example of serializing a Spread sheet using binary serialization:
FileStream m = new FileStream("c:\\\out.xml", FileMode.Open);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
SheetViewCollection ss = default(SheetViewCollection);
try {
ss = (SheetViewCollection)b.Deserialize(m);
m.Close();
FpSpread1.Sheets.Count = 0;
foreach (SheetView sheet in ss) {
FpSpread1.Sheets.Add(sheet);
}
} catch (Exception ex) {
Debug.WriteLine(ex);
}
Refer to the attached samples for complete understanding of the Spread Serialization process.