BlackWaspTM
XML
.NET 1.1+

XML Serialization

With XML serialization, the public state of objects can be converted into an XML document. Such XML information is often stored on disk to persist data for later use, or is transported over a network or the Internet to send messages between computers.

Serialization

The concepts of serialization have already been described in a previous article. For more information, read "Binary Serialization". In this article we will demonstrate the basics of XML serialization. This allows the public state of objects to be converted to XML and later deserialized back into objects.

The main advantage of XML serialization over binary serialization is portability. XML can be interpreted using many development frameworks and languages, and on computers using a variety of operating systems. This allows you to send the XML representation of an object to software applications that are not using the .NET framework or Microsoft Windows. In addition, XML is human-readable and editable, allowing serialized information to be modified using a standard text editor such as Notepad.

There are two key drawbacks of XML serialization. Firstly, only the public state of objects is serialized. Private fields and properties are not represented in the produced XML. Secondly, object references are not maintained when the XML is deserialized into an object; if the original object contained two references to the same object, the deserialization process will generate references to two separate objects.

XML serialization using the .NET framework supports many configuration options that allow the exact structure of the XML to be controlled. However, in this introductory article we will use the default settings for serialization. In future articles we will see how to customise the process further, including changing the names of the XML attributes and elements that are created and using XML schemas.

Serializing To XML

To demonstrate XML serialization we need a sample class. For this, we will use the same Car class that was used in the binary serialization article. The only difference is that the Serializable attribute is not required. The class can be used to represent a car with properties for the model and colour. The class includes a method that allows the number of times that the car has crashed to be incremented, with the count stored in a private field. The ToString method is overridden to allow the state of Car objects to be easily outputted to the Console.

NB: For this example, C#3.0 automatically implemented property syntax has been used. If you are using an earlier language version, expand these to full property declarations with backing store fields.

public class Car
{
    private int _crashes = 0;

    public string Model { get; set; }

    public string Colour { get; set; }

    public void AddCrash()
    {
        _crashes++;
    }

    public override string ToString()
    {
        return string.Format("{0} {1}, {2} crash(es).", Colour, Model, _crashes);
    }
}

Next we need to create a Car object. Add the following code to a console application to create an instance and output its details.

Car car = new Car();
car.Colour = "Red";
car.Model = "Coupe";
car.AddCrash();
Console.WriteLine(car);     // Outputs "Red Coupe, 1 crash(es)."

XML Serialization uses the XmlSerializer class, which is found in the System.Xml.Serialization namespace. In the examples we will be outputting the XML to a file on disk using methods from the System.IO namespace. To simplify the code, add the following two using directives to the code:

using System.IO;
using System.Xml.Serialization;
9 July 2010