.NET Serialization: Binary, SOAP, and XML (VB)


In .NET when you need to shrink an object into a more portable format you have 3 choices.  Binary, SOAP, or XML.  The process of shrinking and expanding the object is called serialization and deserialization.  Each option has it’s own advantages which I will explain.

Binary

You can serialize an object into binary format quite easily using the .NET Framework.  This is the quickest and smallest way to serialize; however, the binary format can only be deserialized using the same .NET Framework.  This may be a problem for interoperability. It must be noted that this format will serialize Public and Private members (unlike XML serialization).

The example below shows how to serialize an object called “newCustomer”:

        Dim newCustomer As Contact = GetSampleContact()

        Using fs As New IO.FileStream(filePathandName & ".bin", IO.FileMode.Create)
            Dim binFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            binFormatter.Serialize(fs, newCustomer)
        End Using

And here is the reverse to deserialize:

        Dim readCustomer As Contact

        Using fs As New IO.FileStream(filePathandName & ".bin", IO.FileMode.Open, IO.FileAccess.Read)
            Dim binFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            readCustomer = CType(binFormatter.Deserialize(fs), Contact)
        End Using

SOAP

SOAP is another method. This is similar to the binary method except the serialized data is stored in a standardized form (SOAP) which will easily allow interoperability. Public and Private members are serialized using this method as well.
To serialize:

        Dim newCustomer As Contact = GetSampleContact()
        Using fs As New IO.FileStream(filePathandName & "SOAP.xml", IO.FileMode.Create)
            Dim soapFormattter As New Runtime.Serialization.Formatters.Soap.SoapFormatter()
            soapFormattter.Serialize(fs, newCustomer)
        End Using

And to deserialize:

        Dim readCustomer As Contact
        Using fs As New IO.FileStream(filePathandName & "SOAP.xml", IO.FileMode.Open, IO.FileAccess.Read)
            Dim soapFormattter As New Runtime.Serialization.Formatters.Soap.SoapFormatter()
            readCustomer = CType(soapFormattter.Deserialize(fs), Contact)
        End Using

XML

A third choice is using the XMLSerializer class. This method will only serialize Public properties and members and the object must be marked with the Serializable attribute and contain an empty constructor. This method of serialization will be smaller in size than the SOAP method since less information is serialized. Here is an example of such class:

     <Serializable()> Class Contact

        Private _FirstName As String
        Public Property FirstName() As String
            Get
                Return _FirstName
            End Get
            Set(ByVal value As String)
                _FirstName = value
            End Set
        End Property

        Private _LastName As String
        Public Property LastName() As String
            Get
                Return _LastName
            End Get
            Set(ByVal value As String)
                _LastName = value
            End Set
        End Property

        Private _MiddleI As String
        Public Property MiddleI() As String
            Get
                Return _MiddleI
            End Get
            Set(ByVal value As String)
                _MiddleI = value
            End Set
        End Property

        Private _PIN As String
        Public WriteOnly Property PIN() As String
            Set(ByVal value As String)
                _PIN = value
            End Set
        End Property

        Sub New()

        End Sub

        Sub New(ByVal fName As String, ByVal middleI As String, ByVal lName As String, ByVal p As String)
            _FirstName = fName
            _MiddleI = middleI
            _LastName = lName
            _PIN = p
        End Sub
    End Class

Here is how you would serialize using the XMLSerializer:

        Dim newCustomer As Contact = GetSampleContact()
        Using fs As New IO.FileStream(filePathandName & ".xml", IO.FileMode.Create)
            Dim xmlFormatter As New Xml.Serialization.XmlSerializer(GetType(Contact))
            xmlFormatter.Serialize(fs, newCustomer)
        End Using

And to deserialize:

        Dim readCustomer As Contact
        Using fs As New IO.FileStream(filePathandName & ".xml", IO.FileMode.Open, IO.FileAccess.Read)
            Dim xmlFormatter As New Xml.Serialization.XmlSerializer(GetType(Contact))
            readCustomer = CType(xmlFormatter.Deserialize(fs), Contact)
        End Using

Serialization Demo

You can download a sample serialization demo project that I created to demonstrate: SerializationDemoProject.zip.

, , , , ,

Comments are closed.