Click here to Skip to main content
13,348,514 members (90,740 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 6 Jul 2010

Mini XML serializer

, 6 Jul 2010
Rate this:
Please Sign up or sign in to vote.
A small XML serializer for Silverlight projects.


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:

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:

public void OnSerialize(MiniSerializer serializer)
    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:

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

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

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.


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


About the Author

Alain Bertrand
Software Developer (Senior) Paul Scherrer Institute
Switzerland Switzerland
Full time developer in a Gov. research institute, I invest also some time to develop games.

I currently own since 5 years a PHP / AJAX MMORPG:

And developing a MMO space combat trader game in Silverlight:

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
DarkRisingForce6-Jul-10 7:04
memberDarkRisingForce6-Jul-10 7:04 
GeneralRe: My vote of 4 Pin
a_bertrand6-Jul-10 8:08
membera_bertrand6-Jul-10 8:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 6 Jul 2010
Article Copyright 2010 by Alain Bertrand
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid