Definitions
For a C# object to be transmitted or stored in memory, files, or databases, the C# object must be converted to a stream of bytes. Serialization is the process of converting an object into bytes so that it can be transmitted or persisted.
XML serialization serializes the public fields and properties of an object into an XML stream that conforms to a specific XML document. The reverse process is called deserialization.
Example
In this example, I'm going to write a program for the property listings for sale in the city of Melbourne. My listings object has a collection of postcodes. Each postcode is assigned with an id and a collection of properties in that postcode. Each property is assigned with an id, several beds, baths, and cars. The listing data can be deserialized from an XML string to a C# object and be serialized from a C# object to an XML string.
Below is my sample XML data:
<listings>
<postcode id="3000">
<properties>
<property id="1">
<beds>4</beds>
<baths>2</baths>
<cars>2</cars>
</property>
<property id="2">
<beds>3</beds>
<baths>1</baths>
<cars>1</cars>
</property>
</properties>
</postcode>
<postcode id="3001">
<properties>
<property id="3">
<beds>2</beds>
<baths>1</baths>
<cars>0</cars>
</property>
<property id="4">
<beds>5</beds>
<baths>3</baths>
<cars>3</cars>
</property>
</properties>
</postcode>
</listings>
This data can be deserialized into a C# object in many ways. The easiest way is to use the XmlSerializer with binding model classes. To convert the previous sample data to binding models, you can use this free XML to C# online converter to get the following result:
[XmlRoot(ElementName = "property")]
public class Property {
[XmlElement(ElementName = "beds")]
public int Beds {
get;
set;
}
[XmlElement(ElementName = "baths")]
public int Baths {
get;
set;
}
[XmlElement(ElementName = "cars")]
public int Cars {
get;
set;
}
[XmlAttribute(AttributeName = "id")]
public int Id {
get;
set;
}
[XmlText]
public int Text {
get;
set;
}
}
[XmlRoot(ElementName = "properties")]
public class Properties {
[XmlElement(ElementName = "property")]
public List <Property> Property {
get;
set;
}
}
[XmlRoot(ElementName = "postcode")]
public class Postcode {
[XmlElement(ElementName = "properties")]
public Properties Properties {
get;
set;
}
[XmlAttribute(AttributeName = "id")]
public int Id {
get;
set;
}
[XmlText]
public int Text {
get;
set;
}
}
[XmlRoot(ElementName = "listings")]
public class Listings {
[XmlElement(ElementName = "postcode")]
public List <Postcode> Postcode {
get;
set;
}
}
Deserialization
We can start writing the code to deserialize the XML string to a listings object.
XmlSerializer serializer = new XmlSerializer(typeof(Listings));
using (StringReader reader = new StringReader(xml))
Listings listings = (Listings)serializer.Deserialize(reader);
First, we create an instance of the XmlSerializer class with the Listings type. Then, we create an instance of the StringReader class from the XML data. Finally, we pass the reader object to the Deserialize method of the serializer object and cast the result to the Listings type to get the listings object.
If the XML data is stored in a file in your solution, you can also use the PhysicalFileProvider to get the info from the file path and use it to create a new stream for the deserialization as follows:
XmlSerializer serializer = new XmlSerializer(typeof(Listings));
var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
var file = fileProvider.GetFileInfo(filePath);
using Stream stream = file.CreateReadStream();
Listings listings = (Listings)serializer.Deserialize(stream);
Serialization
XmlSerializer serializer = new XmlSerializer(typeof(Listings));
using(StringWriter writer = new StringWriter())
serializer.Serialize(writer, listings);
string xml = writer.ToString();
To serialize a C# object to an XML string, you also need to instantiate a new serializer of the XmlSerializer type. However, this time you create a new StringWriter object and use it to call the Serialize method of the serializer to serialize the listings object. Finally, let's call the ToString method of the StringWriter object to get the string XML data.
Conclusion
Object serialization and deserialization are the basics of programming that every programmer should know. There are several ways to achieve these tasks. The simplest way to deserialize and serialize objects programmatically in C# is to use the XmlSerializer. Perhaps, you may want to make these methods static helpers and reuse them throughout the solution.
Let me know if you think this post is helpful. Happy coding!