Note the use of the "Serializable" attribute. This tells the serialization object that these structures are allowed to be serialized. The "Serializable" attribute is not required when using XML serialization, but it is still a good idea to designate it.
We can also prevent a field from being serialized. This is useful when the field value has no specific meaning except in this instance of the application. The designation of a field with the "NonSerialized" (for binary serialization) or "Serialization.XMLIgnore" (for XML serialization) will prevent a field from being serialized. Since we are doing both binary and XML serialization, we have to apply both attributes.
Serializing our Object
To perform the serialization of our object only takes a few lines of code. We first must create a serializing object. In our case, this is an object created from the Serialization.XMLSerializer or BinaryFormatter class. We also create a stream (of type FileStream) in which to store the object. Then we call the Serialize method or our serializing object to do the work, then we close the stream.
The code looks like this:
'XML Serialization
Dim Serializer As New Serialization.XmlSerializer(GetType(Employees))
Dim DataFile As New FileStream("Sample.dat", FileMode.Create, _
FileAccess.Write, FileShare.None)
Serializer.Serialize(DataFile, Data)
DataFile.Close()
Data = Nothing
'Binary Serialization
Dim Serializer As New BinaryFormatter
Dim DataFile As New FileStream("Sample.bin", FileMode.Create, _
FileAccess.Write, FileShare.None)
Serializer.Serialize(DataFile, Data)
DataFile.Close()
Data = Nothing
The last line in both cases is merely to empty the instance of our data class to demonstrate that when we deserialize the stream into our object we are actually getting data from the file and not from something remaining in memory.
Notice also the inclusion of the following Imports statements at the top of the form:
Imports System.Xml
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Deserializing
To deserialize, the code is almost identical as the serialization code except we call the Deserialize method or the serialization object:
'XML Deserialization
Dim Deserializer As New Serialization.XmlSerializer(GetType(Employees))
Dim DataFile As New FileStream("Sample.dat", FileMode.Open, _
FileAccess.Read, FileShare.None)
Data = CType(Deserializer.Deserialize(DataFile), Employees)
DataFile.Close()
'Binary Deserialization
Dim Deserializer As New BinaryFormatter
Dim DataFile As New FileStream("Sample.bin", FileMode.Open, _
FileAccess.Read, FileShare.None)
Data = CType(Deserializer.Deserialize(DataFile), Employees)
DataFile.Close()
Running The Sample Solution
Now that we have reviewed how it is done, take a look at it in action.

Run the sample code and press the "Show Object Data" button. It will show the current contents of the Employees object that is loaded as part of the Form_Load event.

Now serialize the object using the "Serialize to XML" or "Serialize to Binary" buttons. Press the "Show Object Data" button and it will tell us the object is empty.

If we deserialize the stream, and press the "Show Object Data" button again we can see we have our data back. When running these examples, take note of the salary value. Since we designated this field to not be serialized, it is zero after deserializing.

Also, take a look at the two output files ("Sample.xml" and "Sample.bin"). These files could be used by other applications or reused by our application. The XML file, shown here, could easily be used by other applications or used to create an HTML page by applying an XSL style sheet:
<?xml version="1.0"?>
<EMPLOYEES xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<WORKERS>
<EMPLOYEE>
<NAME>Marc Butenko</NAME>
<ID>1</ID>
<POSITION>Computer Systems Analyst</POSITION>
</EMPLOYEE>
<EMPLOYEE>
<NAME>Bill Gates</NAME>
<ID>0</ID>
<POSITION>CEO</POSITION>
</EMPLOYEE>
<EMPLOYEE>
<NAME>John Doe</NAME>
<ID>2</ID>
<POSITION>Janitor</POSITION>
</EMPLOYEE>
</WORKERS>
</EMPLOYEES>
Summary
Other uses of the serialization object are also available. If we had chosen to serialize the object to a memory stream, we could then write the data to an encrypted file stream to prevent the data from being viewed by prying eyes.
Now that we have covered the basics of serialization, try experimenting with it using different serialization classes and attributes. I often use it in simple applications to provide a quick and easy data storage solution. Hopefully you have found this educational and enlightening and can make use of the material here in your applications.