Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Serializing & Deserializing Objects Using Standard and Binary Formatter in VB.Net

Author: DevASP
Download Source Code : 398_VB.NET - Advanced - Serialization - How-To Serializing Objects.zip

This Article demonstrates how to serialize and deserialize an object to/from a stream. This Article defines how we can allow the user to serialize two classes, standard/binary serialization.

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 Sub

#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

 

End Sub

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)

 

Get a SOAP Formatter instance

Dim sf As New SoapFormatter()

 

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)

fs.Close()

 

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

 

End Sub

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

 

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 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)

 

Close the file and release resources (avoids GC delays)

fs.Close()

 

Deserialization is now available

cmdStandardDeserializationBinary.Enabled = True

 

End Sub

 

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

     

Dim c As Class1

 

Get a filestream that reads from strFilename2

Dim fs As New FileStream(strFileName2, FileMode.Open)

 

Get a Binary Formatter instance

Dim bf As New BinaryFormatter()

 

Deserialize c from strFilename2 Note that the deserialized object must be cast to the proper type.

     

c = CType(bf.Deserialize(fs), Class1)

 

Close the file and release resources (avoids GC delays)

fs.Close()

 

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 button after deserializing

cmdStandardDeserializationBinary.Enabled = False

 

End Sub

Note: Source Code is Given With That Article.

Article Comments
Hi,
Nice post. I tried your code on a collection class and serializes the class with no problem. Only when deserializing I get an error saying that "The constructor to deserialize an object of type 'MyClass' was not found". Any hint on that?

My class stores a collection of a custom structure.

Thanks

Posted on 11/11/2008 5:15:43 AM by Al

source code for serialization

Posted on 3/4/2010 12:13:12 AM by karthik kumar

think's freinds

Posted on 5/8/2010 7:20:28 PM by rachid

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to rotate an image in Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net