Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Mini XML serializer

4.80/5 (5 votes)
6 Jul 2010CPOL2 min read 26K   209  
A small XML serializer for Silverlight projects.

Introduction

Silverlight is certainly an incredible framework, and yet in some areas, lacks what is needed. During my Silverlight journey, as I was writing a game, I had the requirement to save the current game state; however, there was no real solution available, or at least I didn't find any. This situation is also partially due to my own requirements like saving the events and having a full class hierarchy without yet defining the list of implementing classes like the standard XML serialization works on .NET.

To solve this problem, I wrote myself my little XML serializer which is fully compatible with Silveright applications (and the limitations of Silverlight) as well as normal .NET applications. It will serialize any class which implements the IMiniSerializable interface and some of the basic types. It will serialize generic lists and events. Circular references are handled as a pointer to known objects. Finally, all this is done with the thought of reducing as much as possible the XML size (at the cost of readability of the XML).

Using the Code

The attached zip file contains the serializer class, the interface, and an example of how it can be used. The project is a .NET 2 console project, but can be used in nearly any .NET 2 or above projects.

Each class which needs to be serialized (besides some .NET class like double, int, etc.) must implement the interface IMiniSerializable which requires to implement these functions:

C#
void OnSerialize(MiniSerializer serializer);
void OnDeserialize(MiniSerializer serializer);

These functions are responsible of the serialization of the class itself. Attributes tagging of the properties and fields of the class would not work as Silverlight doesn't have access to protected or private members of a class (due to sandboxing).

A typical usage would be:

C#
public void OnSerialize(MiniSerializer serializer)
{
    serializer.Serialize("name", this.name);
    serializer.Serialize("X", X);
    serializer.Serialize("listPointer", listPointer);
}

public void OnDeserialize(MiniSerializer serializer)
{
    name = (string)serializer.Deserialize("name");
    X = (double)serializer.Deserialize("X");
    listPointer = (DataList)serializer.Deserialize("listPointer");
}

Finally, once you implement all the classes of your object tree, you will be able to serialize the root object via something like:

C#
MiniSerializer serializer = MiniSerializer.GetSerializer(
               new FileStream("test.xml", FileMode.Create));
serializer.Serialize("toto", t1);
serializer.Close();
serializer.DisposeStream();

MiniSerializer deserializer = MiniSerializer.GetDeserializer(
               new FileStream("test.xml", FileMode.Open));
DataList t = (DataList)deserializer.Deserialize("toto");
deserializer.Close();

Points of Interest

This "simple" serialization took me unprepared as to how complex it has been. The main issue has been the limited Silverlight support; for example, the lack of ability to have a full introspection as I was used with .NET. I fully understand the reason, which is the sandboxing model for Silverlight applications, yet it has been a bit painful to work around it.

Another issue which got me totally unprepared was the serialization and de-serialization of events. Here again, I didn't find a lot of information on the web, and again, I got troubles with the introspection issues of Silverlight. However, overall, I think I made a nice, well rounded little class which should do most of what's required, without too much overhead while using it.

Finally, if you really want to reduce the size of your serialized data, you should consider compressing the XML.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)