Serializing & Deserializing Objects with VB.NET 2012

Introduction

I don't need to tell most of you how important serialization is in your programs. If today is the first time you come across the term Serialization, I'd suggest reading here before continuing.

Being able to serialize and deserialize information in your apps is a very important feature to have. Being able to save and load information stored is crucial. There are many ways to serialize data, but it all depends on your program's needs. You can serialize info into and from the registry; you can store and load info from a database, or from a file. Today we will save info to an xml file and load it from that xml file.

Design

We will be using a very basic design, have a look at Figure 1, and design your form similar to it.

Figure 1 - Our Design

Coding

I decided that instead of serializing basic tidbits of data, to throw a spanner in the works and demonstrate how to save real objects created from code. For this demonstration we will create a new class called clsStudent and give it some properties. These properties will be the information we will serialize. Let us add a new class now, and edit its code.

clsStudent

Add the necessary namespace(s), in General Declarations:

Imports System.Xml.Serialization 'The Serialization namespace contains classes that are used to serialize objects into XML format documents or streams

The above member variables will be used in conjunction with our class' properties (which we will add now) to get and set their values. Let us add the Properties now.

'Gets / Sets Student Name
Public Property StudentName() As String
Get
StudentName = strName
End Get
Set(ByVal Value As String)
strName = Value
End Set
End Property
'Gets / Sets Student's Course
Public Property StudentCourse() As String
Get
StudentCourse = strCourse
End Get
Set(ByVal Value As String)
strCourse = Value
End Set
End Property
'Gets / Sets StudentNumber
<XmlElementAttribute(ElementName:="Student Number")> _
Public Property StudentNumber() As Integer
Get
StudentNumber = intStuNo
End Get
Set(ByVal Value As Integer)
intStuNo = Value
End Set
End Property

You can probably tell what the above code does. We simply give our Student class some properties. These properties include StudentName (for the Student's name), StudentCourse (what course the student does) and StudentNumber (the student's student number).

We need to connect our student class, and from there populate it with our form's controls. Finally, we must serialize and deserialize the entered info.

frmSD ( or However You Named It )

Let us start with the namespaces again. I always start with the namespaces - old habits die hard I suppose. Add the following two namespaces to your form's general declarations section:

We created a new clsStudent object, and populated it with whatever data was entered into the textboxes. Note, if you have named your objects differently, you will have to compensate for that in your code.

We then created a StreamWriter object, which will create our file on the C drive, called StudentInfo.xml.

We created an XmlSerializer object that will facilitate in the serializing of our data. Lastly we wrote our entered data, and closed the file.

Same principle as the serialization, but we just load the info with the help of the Deserialize method of the XmlSerializer object and load the info into our textboxes.

If you run your project now, you will be able to save your entered data, and then load them. I am including the source files below.

Conclusion

As always, (you may say that I sound like a broken record sometimes - blame it on my O. C. D.) I hope you have enjoyed this little article, and that you have learned something from it. Until next time! Cheers! - I always say that too...

Top White Papers and Webcasts

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.