1. Home
  2. Computing & Technology
  3. Visual Basic

All About Serializing in Visual Basic
The Serializing Details

By , About.com Guide

Dec 13 2008

One of the first things you should notice about this example is the <Serializable()> attribute in the Class. Attributes are just more information that you can provide to VB.NET about an object and they're used for a lot of different things. For an in depth explanation of attributes, try my four part article about attributes in VB.NET. Read the article here. The attribute in this code tells VB.NET to add extra code so that later on, everything in this class can be serialized.

If there are specific items in the Class that you don't want to be serialized, you can use the <NonSerialized()> attribute to exclude them:

<NonSerialized()> Public Parm3Value As String = "Whatever"

In the example, notice is that Serialize and Deserialize are methods of the BinaryFormatter object (f in this example).

f.Serialize(s, ParmData)

This object takes the FileStream object and the object to be serialized as parameters. We'll see that VB.NET offers another object that allows the result to be expressed as XML.

And one final note, if your object includes other subordinate objects, they'll be serialized too! But since all objects that are serialized must be marked with the <Serializable()> attribute, all of these child objects must be marked that way too.

Just to be completely clear about what is happening in your program, you might want to display the file named ParmData in Notepad to see what serialized data looks like. (If you followed this code, it should be in the bin.Debug folder in your project.) Since this is a binary file, most of the content isn't readable text, but you should be able to see any strings in your serialized file. We'll do an XML version next and you might want to compare the two just to be aware of the difference.

Serializing to XML instead of a binary file requires very few changes. XML isn't as fast and can't capture some object information, but it's far more flexible. XML can be used by just about any other software technology in the world today. If you want to be sure your file structures don't "tie you into" Microsoft, this is a good option to look into. Microsoft is emphasizing "LINQ to XML" to create XML data files in their latest technology but many people still prefer this method.

The 'X' in XML stands for eXtensible. In our XML example, we're going to use one of those extensions of XML, a technology called SOAP. This used to mean "Simple Object Access Protocol" but now it's just a name. (SOAP has been upgraded so much that the original name doesn't fit that well anymore.)

The main thing that we have to change in our subroutines is the declation of the serialization formatter. This has to be changed in both the subroutine that serializes the object and the one that deserializes it again. For the default configuration, this involves three changes to your program. First, you have to add a Reference to the project. Right-click the project and select Add Reference .... Make sure ...

System.Runtime.Serialization.Formatters.Soap

... has been added to the project.

Then change the two statements in the program that references it.

Imports System.Runtime.Serialization.Formatters.Soap

Dim f As New SoapFormatter

This time, if you check out the same ParmData file in Notepad, you'll see that the whole thing is in readable XML text such as ...

<Parm1Name id="ref-3">Parm1 Name</Parm1Name>
<Parm1Value>12345</Parm1Value>
<Parm2Name id="ref-4">Parm2 Name</Parm2Name>
<Parm2Value>54321.12345</Parm2Value>

There is also a lot of additional XML there that's necessary for the SOAP standard in the file as well. If you want to verify what the <NonSerialized()> attribute does, you can add a variable with that attribute and look at the file to verify that it's not included.

Explore Visual Basic
By Category
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Serialize in Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.