Note
Code examples shown in this section require Imports System.IO and Imports System.Xml.Serialization directives.
One of the main goals of
serialization is to provide a way for exchanging data with other
applications so that such applications can re-create objects’ state. If
you want to share your objects with non-.NET applications or with
applications running on different platforms, a convenient way for
serializing objects is provided by the Xml serialization. As you know,
Xml is a standard international file format for data exchange. Xml files
are basically text files organized according to a hierarchical
structure and therefore can be manipulated in whatever platforms and
applications. Xml serialization thus provides two great benefits:
absolute interoperability and background compatibility. If you upgrade
or modify your applications, Xml format remains the same. Opposite to
such benefits, Xml serialization has two limitations: It cannot
serialize object graphs (therefore single objects) and cannot serialize
private members. Xml serialization is performed by taking advantage of
objects exposed by the System.Xml.Serialization namespace. Particularly you can use the XmlSerializer class that requires a System.IO.Stream
object for outputting serialized data and the data itself. The
following code shows how you can serialize a typed collection of strings
using Xml serialization:
Dim stringSeries As New List(Of String) From
{"Serialization", "demo",
"with VB"}
Dim targetFile As New FileStream("C:\temp\SerializedData.xml",
FileMode.Create)
Dim formatter As New XmlSerializer(GetType(List(Of String)))
formatter.Serialize(targetFile, stringSeries)
targetFile.Close()
formatter = Nothing
The XmlSerializer constructor requires the specification of the data type you are going to serialize, which is accomplished via the GetType operator. To serialize data you invoke the XmlSerializer.Serialize
method. As you can see, there are no big differences with other
serialization techniques shown in the previous section. To check how
your data was serialized, you can open the SerializedData.xml file. In
this case you can accomplish this with an Xml editor or with a web
browser instead of Notepad. Figure 1 shows the serialization result within Internet Explorer.
Notice how the newly
obtained file has a perfect Xml structure and therefore can be shared
with other applications having the ability of performing Xml
deserialization. To deserialize your data you simply invoke the XmlSerializer.Deserialize method, as shown in the following code:
Dim sourceFile As New FileStream("C:\temp\SerializedData.xml",
FileMode.Open)
formatter = New XmlSerializer(GetType(List(Of String)))
Dim data = CType(formatter.Deserialize(sourceFile),
List(Of String))
sourceFile.Close()
formatter = Nothing
'Iterates the result
For Each item In data
Console.WriteLine(item)
Next
Customizing Xml Serialization
Consider the following implementation of the Person class:
Public Class Person
Public Property FirstName As String
Public Property LastName As String
Public Property Age As Integer
End Class
When you serialize an instance of that Person class, you would obtain an Xml representation similar to the following:
<?xml version="1.0" ?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Alessandro</FirstName>
<LastName>Del Sole</LastName>
<Age>32</Age>
</Person>
The System.Xml.Serialization
namespace offers attributes for controlling output of the Xml
serialization to affect the target file. For example, see the following
code:
Imports System.Xml.Serialization
<XmlRoot("Contact")> Public Class Person
<XmlIgnore()> Public Property FirstName As String
Public Property LastName As String
<XmlAttribute()> Public Property Age As Integer
End Class
When an instance is serialized, the output looks like the following:
<?xml version="1.0" ?>
<Contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Age="32">
<LastName>Del Sole</LastName>
</Contact>
The XmlRoot attribute changed the name of the root element from Person to Contact. The XmlIgnore attribute prevented a property from being serialized, whereas the XmlAttribute
attribute treated the specified member as an Xml attribute instead of
an Xml element. You can find the complete attributes list in the
dedicated page of the MSDN Library at http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes_members(VS.100).aspx.
The reason why you should get a reference on the Internet is that Xml
serialization is a settled concept for most developers, whereas .NET
Framework 4.0 provides a new, more interesting way for Xml
serialization, known as XAML serialization.