BLOG.CSHARPHELPER.COM: Serialize and deserialize objects in C#
Serialize and deserialize objects in C#
Serialization is the process of converting an object into a serial stream-like format. Often that means converting it into XML data for storage or transmittal over a network.
Deserialization is the process of using a serialization to rebuild the original object.
Basic serialization in C# is relatively simple. First create the class that you want to serialize and decorate it with the Serializable attribute. Define properties as usual. Note that the class must be public and must have a default, empty constructor that takes no parameters. Without those two features, the serializer cannot create objects.
The following code shows this example's Person class.
// The class must be Serializable and public. [Serializable()] public class Person { public string FirstName; public string LastName; public string Street; public string City; public string State; public string Zip;
// Empty constructor required for serialization. public Person() { }
// Initializing constructor. public Person(string first_name, string last_name, string street, string city, string state, string zip) { FirstName = first_name; LastName = last_name; Street = street; City = city; State = state; Zip = zip; } }
The following code shows how the program serializes a Person object.
// Make a new Person. Person per = new Person( txtFirstName.Text, txtLastName.Text, txtStreet.Text, txtCity.Text, txtState.Text, txtZip.Text);
// Make the XmlSerializer and StringWriter. XmlSerializer xml_serializer = new XmlSerializer(typeof(Person)); using (StringWriter string_writer = new StringWriter()) { // Serialize. xml_serializer.Serialize(string_writer, per);
// Display the serialization. txtSerialization.Text = string_writer.ToString(); }
The code first creates a new Person object, passing its constructor values entered in text boxes. It then creates an XmlSerializer object, passing its constructor the type of the object that it will serialize (Person).
Next the code makes a StringWriter object so it can serialize into a string. You could serialize into other stream objects such as file streams if you want.
The code calls the serializer's Serialize method, passing it the stream in which to serialize (the StringWriter) and the Person object to serialize. The program displays the serialization in the txtSerialization text box.
The following text shows the resulting serialization. Notice that the serializer automatically uses the names of the Person object's public variables as tags in the resulting XML.
The following code shows how the program deserializes the serialization to recreate the Person object.
// Deserialize the serialization. XmlSerializer xml_serializer = new XmlSerializer(typeof(Person)); using (StringReader string_reader = new StringReader(txtSerialization.Text)) { Person per = (Person)(xml_serializer.Deserialize(string_reader));
// Display the Person's properties in the TextBoxes. txtFirstName.Text = per.FirstName; txtLastName.Text = per.LastName; txtStreet.Text = per.Street; txtCity.Text = per.City; txtState.Text = per.Street; txtZip.Text = per.Zip; }
The code creates a new XmlSerializer for Person objects as before. It makes a StringReader initialized to hold the previously created serialization.
Next the code calls the serializer's Deserialize method, passing it the stream (StringReader) from which it should read the serialization. Deserialize returns a generic object so the code casts the result into the Person data type. It then displays the Person's field values in the text boxes.
Comments