Writing Windows Apps With Visual Basic .NET: XML
In most cases, in your Visual Basic .NET applications, you may just want to create objects, get a task completed and then remove the objects from memory. However, in some cases, you may want to take the complete state of an in-memory object and preserve it on disk so that you can re-create it later. Or you may want to transmit the objects to another application over the network.
Before an object can be persisted on disk or transmitted over the wire, its in-memory representation must be converted into a sequence of bytes. This process is known as serialization. A reverse process by which you take a sequence of bytes and re-create the objects in memory is known as deserialization.
Looking at the .NET Framework, you’ll find many examples of serialization in action. For example, ASP.NET uses serialization to save out-of-process session state. Remoting also uses serialization to pass objects from one application domain to another. The .NET Framework provides various ways to serialize an object. For example, you can have an object serialized into binary format or in XML format. The .NET Framework also offers extensibility by allowing you to define a custom-serialization format.
This article focuses on the built-in XML serialization techniques offered by the .NET Framework. By serializing an object to XML format you can benefit from the many advantages that XML offers, such as open architecture and interoperability. There are two ways to persist an object to the XML format: using the XmlSerializer class of the System.Xml.Serialization namespace or using the SoapFormatter class of the System.Runtime.Serialization namespace.
In addition, some .NET Framework classes may also provide specific functionality for XML serialization. One such class is the DataSet class of the System.Data namespace. This article also explores the XML serialization support offered by the DataSet class.
In many of the examples in this article, you will use an Employee object to demostrate the process of serialization and deserialization. The following listing shows the code for the Employee class:
Public Class Employee
Private _id As Integer
Private _name As String
Public Sub New()
_id = -1
_name = “”
Public Sub New(ByVal Id As Integer, ByVal Name As String)
_id = Id
_name = Name
Public Property Id() As Integer