Please navigate to the bottom of the page for Table of Contents

Wednesday, May 25, 2011

How do you serialize an object to and from XML

Technorati Tags: ,,,

For an ASP.NET object to be saved in a session, it needs to be serialized (not entirely correct – in proc session state allows objects to be non-serializable). Some other use cases are when you want to send and receive objects over the wire (web services) or when you perhaps want to save them to Isolated Storage (Windows Phone 7). The basic question is: How do you enable serialization/ de-serialization of .NET objects?

The answer is to use the XmlSerializer class. There are certain restrictions on the objects that can serialized to and from XML. Before we discuss those properties, let's explore the basic functions to serialize/de-serialize any object:

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace SimpleCodingQuestions
{
class SerializationHelper
{
/// <summary>
/// Serializes the given object data to string representation
/// </summary>
/// <param name="dataToSerialize">the object data to serialize</param>
/// <param name="objectType">the object class type</param>
/// <returns>a string representation for demo purposes</returns>
public static string Serialize(object dataToSerialize, Type objectType)
{
string objAsString = null;
// instantiate an XmlSerializer class
XmlSerializer xs = new XmlSerializer(objectType);
// for the purpose of explaining,
// we will be writing to a string
// in real world, this might be writing to a file
StringBuilder sb = new StringBuilder();
// wrap the serialization
using (StringWriter sw = new StringWriter(sb))
{
// now serialize the data
xs.Serialize(sw, dataToSerialize);
}

return objAsString;
}
}
}

As you can see from the program above, you first create an XmlSerializer object with the typeof object you want to serialize. Then you simply call the Serialize method on it.

 

Now let's review the de-serialization code. It is very similar to the above code.

 

public static object Serialize(string serializedData, Type objectType)
{
object returnObj = null;
// instantiate an XmlSerializer class
XmlSerializer xs = new XmlSerializer(objectType);
// wrap the serialization
using (StringReader sr = new StringReader(serializedData))
{
// now de-serialize the data
returnObj = xs.Deserialize(sr);
}

return returnObj;
}

 

So, what are the restrictions on the objects that can be serialized to and from XML?


  • Only public types and public data members will be used.

  • The class needs to provide a no-arg (parameterless) constructor.

  • If the class is made up of complex objects, only public types of those types will be serialized

How can you modify/control the XML that is being generated by XmlSerializer?


Although the process of XML generation is straightforward, .NET framework provides you with many ways to customize the output. You can control the generation by using attributes that can decorate the public types and properties. A few examples are:



  • Use [XmlIgnore] attribute to exclude a certain property from being serialized

  • Use [XmlElement("DifferentElementName")] to rename a property to another name in the generated XML.

  • Use [XmlAttribute("DifferentNameAsAttribute")] to rename a property to another name in the generated XML and also to serialize it as an attribute of the XmlElement as compared to a sub element of the main element.

There are many other properties and attributes that can be configured for an XmlSerializer, making it a very powerful and versatile tool for a developer.


 

 


 

No comments:

Post a Comment