XML Serialization

The simplest way to demonstrate Visual Basic's support for XML is to use it to serialize a class. The serialization of an object means that it is written out to a stream, such as a file or a socket. The reverse process can also be performed: an object can be deserialized by reading it from a stream and creating the XML from that stream. You may want to do this to save an object's data to a local file, or to transmit it across a network.

Note
The type of serialization described in this chapter is XML serialization, whereby XML is used to represent a class in serialized form. You will see other forms of serialization in the WCF chapter (Chapter 11).

To help you understand XML serialization, let's examine a class named FilmOrder in the FilmOrder project. This class could be used by a company for processing a movie order.

An instance of FilmOrder corresponding to each order could be serialized to XML and sent over a socket from a client's computer. We are talking about data in a proprietary form here: an instance of FilmOrder being converted into a generic form—XML—that can be universally understood.

The System.Xml.Serialization namespace contains classes and interfaces that support the serialization of objects to XML, and the deserialization of objects from XML. Objects are serialized to documents or streams using the XmlSerializer class.

Serializing

Let's look at how you can use XmlSerializer. To make the sample simpler, you'll use a console application. ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.