Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSerialize and deserialize objects in Visual Basic .NET
DescriptionThis example shows how to serialize and deserialize objects in Visual Basic .NET.
Keywordssetrialize, deserialize, serialization, XmlSerializer, VB.NET
CategoriesSoftware Engineering
 
Basic serialization in Visual Basic .NET is relatively simple. First create the class that you want to serialize and decorate it with the Serializable attribute. Define properties as usual. Note that the class must have a default, empty constructor that takes no parameters because the serializer uses it to deserialize objects.
 
Imports System.Xml.Serialization

<Serializable()> _
Public Class Person
    Public FirstName As String
    Public LastName As String
    Public Street As String
    Public City As String
    Public State As String
    Public Zip As String

    ' Empty constructor required for serialization.
    Public Sub New()
    End Sub

    Public Sub New(ByVal new_FirstName As String, ByVal _
        new_LastName As String, ByVal new_Street As String, _
        ByVal new_City As String, ByVal new_State As _
        String, ByVal new_Zip As String)
        FirstName = new_FirstName
        LastName = new_LastName
        Street = new_Street
        City = new_City
        State = new_State
        Zip = new_Zip
    End Sub
End Class
 
The following code serializes a Person object. First it creates a new Person object, passing its constructor values entered in text boxes. It then creates an XmlSerializer object, passing its constructor the type of the object that it will serialize (Person).

The code makes a StringWriter object so it can serialize into a string. You could serialize into other stream objects such as file streams if you want.

The code calls the serializer's Serialize method, passing it the stream to serialize into (the StringWriter) and the Person object to serialize. The program displays the serialization in a text box and then closes the StringWriter.

 
Dim per As New Person( _
    txtFirstName.Text, _
    txtLastName.Text, _
    txtStreet.Text, _
    txtCity.Text, _
    txtState.Text, _
    txtZip.Text)

Dim xml_serializer As New XmlSerializer(GetType(Person))
Dim string_writer As New StringWriter
xml_serializer.Serialize(string_writer, per)

txtSerialization.Text = string_writer.ToString()

string_writer.Close()
 
The following text shows the result. Notice how the serializer automatically used the names of the Person object's public variables as tags in the resulting XML.
 
<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <FirstName>Mickey</FirstName>
  <LastName>Moose</LastName>
  <Street>1337 Leet St</Street>
  <City>Bugsville</City>
  <State>CA</State>
  <Zip>12345</Zip>
</Person>
 
The following code deserializes the serialization string to recreate the Person object. It creates a new XmlSerializer for Person objects as before. It makes a StringReader initialized to hold the previously created serialization.

The code then calls the serializer's Deserialize method, passing it the stream (StringReader) from which it should read the serialization. Deserialize returns a generic object so the code uses DirectCast to convert it into a Person object.

 
Dim xml_serializer As New XmlSerializer(GetType(Person))
Dim string_reader As New StringReader(txtSerialization.Text)
Dim per As Person = _
    DirectCast(xml_serializer.Deserialize(string_reader), _
        Person)
string_reader.Close()
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated