This Article defines how we can allows the user to serialize two classes, one with standard serialization, and one with custom serialization. The six grouped command buttons are for serializing and deserializing. The bottom two buttons allow the user to view the SOAP envelopes for the serialized objects. The text boxes on the right allow the user to specify the initial data for the instances. The read-only textboxes on the far right allow the user to see the new field values after deserialization.
Option Strict On
Allow Namespace for Filestreams
Imports System.IO
Allow Namespace for BinaryFormatter
Imports System.Runtime.Serialization.Formatters.Binary
Need to reference System.Runtime.Serialization.Formatters.Soap for this Import
Imports System.Runtime.Serialization.Formatters.Soap
These variables are initialized in the Form_Load event.
Private strFileName1 As String
Private strFileName2 As String
Private strFileName3 As String
Include one region as given Below. System.Diagnostics.DebuggerStepThrough() has been added to some procedures since they are not the focus of the demo. Remove them if you wish to debug the procedures. This code simply shows the About form.
#Region " Standard Menu Code "
<System.Diagnostics.DebuggerStepThrough()> Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
Open the About form in Dialog Mode
Dim frm As New frmAbout()
frm.ShowDialog(Me)
frm.Dispose()
End Sub
This code will close the form.
<System.Diagnostics.DebuggerStepThrough()> Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
Close the current form
Me.Close()
#End Region
Standard Serialization SOAP
After that write routine cmdStandardSerializationSoap_Click. This routine creates a new instance of Class1, then serializes it to the file Class1File.xml with the SOAP Formatter.
Private Sub cmdStandardSerializationSoap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles cmdStandardSerializationSoap.Click
Create the object to be serialized
Dim c As New Class1(CInt(txtX.Text), CInt(txtY.Text), CInt(txtZ.Text))
Get a filestream that writes to the file specified by strFileName1
Dim fs As New FileStream(strFileName1, FileMode.OpenOrCreate)
Get a SOAP Formatter instance
Dim sf As New SoapFormatter()
Serialize c to strFileName1
sf.Serialize(fs, c)
Close the file and release resources (avoids GC delays)
fs.Close()
Deserialization is now available
cmdStandardDeserializationSoap.Enabled = True
cmdViewClass1.Enabled = True
Standard Deserialization SOAP
This routine deserializes an object from the file Class1File.xml and assigns it to a Class1 reference. Declare the reference that will point to the object to be deserialized.
Private Sub cmdStandardDeserializationSoap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles cmdStandardDeserializationSoap.Click
Dim c As Class1
Get a filestream that reads from strFileName1
Dim fs As New FileStream(strFileName1, FileMode.Open)
Deserialize c from strFileName1 Note that the deserialized object must be cast to the proper type.
c = CType(sf.Deserialize(fs), Class1)
close the file and release resources (avoids GC delays)
Put the deserialized values for the fields into the textboxes
txtXAfter.Text = CStr(c.x)
txtYAfter.Text = CStr(c.GetY)
txtZAfter.Text = CStr(c.z)
Reset buttons after deserializing
cmdStandardDeserializationSoap.Enabled = False
cmdViewClass1.Enabled = False
Binary Formatter Serialization
This routine creates a new instance of Class1, then serializes it to the file Class1File.dat using the Binary formatter.
Private Sub cmdStandardSerializationBinary_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles cmdStandardSerializationBinary.Click
Get a filestream that writes to strFilename2
Dim fs As New FileStream(strFileName2, FileMode.OpenOrCreate)
Get a Binary Formatter instance
Dim bf As New BinaryFormatter()
Serialize c to strFileName2
bf.Serialize(fs, c)
cmdStandardDeserializationBinary.Enabled = True
Binary Formatter Deserialization
This routine deserializes an object from the file Class1File.dat and assigns it to a Class1 reference. Declare the reference that will point to the object to be deserialized.
Private Sub cmdStandardDeserializationBinary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStandardDeserializationBinary.Click
Get a filestream that reads from strFilename2
Dim fs As New FileStream(strFileName2, FileMode.Open)
Deserialize c from strFilename2 Note that the deserialized object must be cast to the proper type.
c = CType(bf.Deserialize(fs), Class1)
Reset button after deserializing
cmdStandardDeserializationBinary.Enabled = False