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
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
private members of a class (due to sandboxing).
A typical usage would be:
public void OnSerialize(MiniSerializer serializer)
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));
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.