Binary and XML Serialization - Binary Serialization
(Page 2 of 4 )
With binary serialization, the public and private fields of the object, in addition to the class name and the assembly, are converted to a stream of bytes. The process of deserialization will rebuild the original object from the stream. Binary serialization is very efficient, but it is limited to the .NET framework. By that I mean I cannot use a Java application to deserialize an object serialized by a .NET-compatible language. With XML serialization, on the other hand, it is doable since the object is serialized as an XML file, which other languages (not just .NET) will be able to read.
The first step in serializing an object is to use the[Serializable]attribute. This attribute is placed above the class declaration. When designing applications, it is very important to decide which classes will be serializable as early in the development process as possible, since making the change later will be more complicated. When in doubt, always mark your class as serializable. Let's start with the[Serializable]attribute. To declare a class serializable, do the following:
[Serializable]
class Book
{
private string isbn;
public string ISBN
{
get { return isbn; }
set { isbn = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
public Book()
{
isbn = "";
title = "";
author = "";
}
public Book(string isbn, string title, string author)
{
this.isbn = isbn;
this.title = title;
this.author = author;
}
public override string ToString()
{
return string.Format("ISBN: {0}, Title: {1}, Author: {2}",
isbn, title, author);
}
}
The book class contains the usual declaration with the isbn, title, and author fields. The corresponding properties provide public access to the private fields. We added two constructors and overrode theToString()method. The only addition here is the[Serializable]attribute added to the class declaration. This attribute is signaling to the .NET Framework that objects of this class could be written to a storage medium.
Please note that the serializable attribute is not inherited. So, if class B inherits from class A, and A is [Serializable], B will NOT automatically be [Serializable]. You need to add the[Serializable]attribute to B as well.
Now that the class is serializable, let's store an instance of this class in a file. To do so, we use theBinaryFormatterclass:
IFormatter formatter = new BinaryFormatter();
Since the instance of the class is stored in a file, it is clear that we need a stream:
Stream stream = new FileStream("book.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
This declaration specifies that we are creating aFileStreamfor writing, and the file name is book.bin. The last step is to call theSerialize()method of theBinaryFormatter to write the object to the file:
formatter.Serialize(stream, favoriteBook);
We can place those calls in a handy method that we can call any time we need to serialize a book object:
private static void SerializeBook(Book favoriteBook)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("book.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, favoriteBook);
stream.Close();
}
Next: Binary Serialization continued >>
More .NET Articles
More By Ayad Boudiab