Over a million developers have joined DZone.

Windows Phone 7 Serialization: DataContract Serialization

DZone's Guide to

Windows Phone 7 Serialization: DataContract Serialization

· Mobile Zone ·
Free Resource

In this post I am going to continue studying the ways how to serialize objects in Windows Phone 7 applications. This time I will explain DataContract Serialization using DataContractSerializer class. This type of serialization can also be applied to Silverlight applications.

Additional Information

Adding a reference

Keep in mind that DataContractSerializer requires System.Runtime.Serialization namespace to be added to project. To do so right click the project name and choose Add Reference there. Select System.Runtime.Serialization namespace on .NET tab (check image bellow).

eugenedotnet add system runtime serialization namespace for data contract serialization


Creating a sample class

I am going to use almost the same class I’ve created for the XmlSerializer tutorial, but slightly modified. To allow object to be serialized using DataContract serialization you have to add DataContractAttribute to the class (public class). XML Generation can be controlled using DataMemberAttribute for those properties that need to be serialized and IgnoreDataMemberAttribute for those properties that need to be ignored during serialization. Public properties will be automatically exposed. Take a look at example bellow.

public class SampleData
    public string ContentText { get; set; }
    public List<int> SomeItems { get; set; }
    public SampleData()
        ContentText = "some text";
        SomeItems = new List<int>() { 1, 2, 3 };


To serialize an object you need to create an instance of DataContractSerializer class (passing a type of serialized object as an input parameter) and then write serialized data to a stream object using WriteObject method, that accepts stream and object marked with DataContractAttribute attribute as input parameters.

public static void Serialize(Stream streamObject, object objForSerialization)
    if (objForSerialization == null || streamObject == null)
    DataContractSerializer ser = new DataContractSerializer(objForSerialization.GetType());
    ser.WriteObject(streamObject, objForSerialization);


Deserialization is also very simple. To deserialize an object you need to create an instance of DataContractSerializer class again (pass constructor a type of serialized object) and then read serialized data out of a stream object using ReadObject method, that accepts stream object as parameter.

public static object Deserialize(Stream streamObject, Type serializedObjectType)
    if (serializedObjectType == null || streamObject == null)
        return null;
    DataContractSerializer ser = new DataContractSerializer(serializedObjectType);
    return ser.ReadObject(streamObject);


I have used the following method to test serialization and deserialization using DataContractSerializer. As the result instance of SampleData class after deserialization is exactly the same as the one before the serialization.

public static void Test()
    // serialization
    MemoryStream ms = new MemoryStream();
    DataContractSerializationHelper.Serialize(ms, new SampleData());
    ms.Position = 0;
    // deserialization
    var sampleData = DataContractSerializationHelper.Deserialize(ms, typeof(SampleData));

Here is how serialized instance of SampleData class looks like:

<SampleData xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
<ContentText>some text</ContentText>
<SomeItems xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}