2005-08-16
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 3 of 3 )
Binary Serialization
Binary serialization is not, as some people erroneously think, simply a matter of creating a byte-for-byte copy of an object's memory image and writing it to disk. Binary serialization must include nested objects as well as type information.
All this is required so that when the object is deserialized, you get an accurate and fully functional replica of the original.
The major limitation of binary serialization is that it is platform-specific, meaning that both the serialization and deserialzation must be on the .Net platform.
Creating a Serializable Class
There's nothing special about a serializable class except that it is marked with the serializable attribute. In Visual Basic it looks like this:
<Serializable()> Public Class ClassName ... End Class
Here's an example serializable class that I will use in the upcoming demos:
<Serializable()> Public Class Person
Public FirstName As String
Public LastName As String
Public ReadOnly Property Fullname()
Get
Return FirstName & " " & LastName
End Get
End Property
End Class
Serializing an Object
Serializing an object is almost trivial, with the tools included in the Framework. All you need to do is create a stream associated with the target file, create an instance of the appropriate formatter (binary or SOAP), and call the Serialize() method. For binary serialization, you need these Import statements:
Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Binary
This code uses the Person class from above. It loads the members with data from text boxes and then serializes the object to a file named mydata.bin. The BIN extension is not required, but does serve to indicate that the file contains binary data.
Dim Someone As New Person
Someone.FirstName = TextBox1.Text
Someone.LastName = TextBox2.Text
Dim fStream As FileStream
Try
fStream = New FileStream("c:\mydata.bin", FileMode.Create)
Dim bfmtr As New BinaryFormatter
bfmtr.Serialize(fStream, Someone)
Catch ex As SerializationException
MsgBox("Failed to serialize because " & ex.Message)
Throw
Finally
fStream.Close()
End Try
SOAP serialization is essentially the same process. You need these Import statements:
Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Soap
For some reason, using the SOAP formatter requires that you add a reference to System.Runtime.Serialization.Formatters.Soap.dll to the project (use the Project, Add Reference command; then click the .Net tab in the Add Reference dialog box). You do not need to add a reference to use the binary formatter.
The serialization code is as follows, writing the data to the file mydata.xml:
Dim Someone As New Person
Someone.FirstName = TextBox1.Text
Someone.LastName = TextBox2.Text
Dim fStream As FileStream
Try
fStream = New FileStream("c:\mydata.xml", FileMode.Create)
Dim XMLfmtr As New SOAPFormatter
XMLfmtr.Serialize(fStream, Someone)
Catch ex As SerializationException
MsgBox("Failed to serialize because " & ex.Message)
Throw
Finally
fStream.Close()
End Try
Deserializing an Object
Deserializing is almost as easy as serializing. The only added factor is the need to cast the data read from the file to the proper type, easily done with the DirectCast() function. Here's the code to deserializze the binary data to a new instance of the Person class and display the retrieved data in a text box:
Dim SomeoneElse As New Person
Dim fStream As FileStream
Dim bfmtr As New BinaryFormatter
Try
fStream = New FileStream("c:\mydata.bin", FileMode.Open)
SomeoneElse = DirectCast(bfmtr.Deserialize(fStream), Person)
Catch ex As SerializationException
MsgBox("Failed to deserialize because " & ex.Message)
Throw
Finally
fStream.Close()
End Try
TextBox3.Text = SomeoneElse.Fullname
And here is the almost identical code for SOAP deserialization:
Dim SomeoneElse As New Person
Dim fStream As FileStream
Dim XMLfmtr As New SoapFormatter
Try
fStream = New FileStream("c:\mydata.xml", FileMode.Open)
SomeoneElse = DirectCast(XMLfmtr.Deserialize(fStream), Person)
Catch ex As SerializationException
MsgBox("Failed to deserialize because " & ex.Message)
Throw
Finally
fStream.Close()
End Try
TextBox3.Text = SomeoneElse.Fullname
Serialization is a very useful technique. Once I learned about it, I was surprised to find how many situations I could use it in. Its utility is further enhanced by the fact that most classes in the .Net Framework are serializable. You can tell by looking for the <serializable> attribute in the class definition in the documentation, as shown here for the Bitmap class:
<Serializable> <ComVisible(True)> NotInheritable Public Class Bitmap Inherits Image
All in all, I think that the .Net Framework's support for serialization is one of the biggest programming time-savers around.
![]() |
|


