This is a class built on the articles of Shawn Hargreaves for using the IntermediateSerializer class to serialize collections of shared resources. In his article he only does the List<T> collection, but I had the need to serialize a Dictionary<T,K> instead, and maybe someone else will need it at some point, so here it is.
The class is to be used with the IntermediateSerializer. It’s a class for serializing data to XML in XNA, and though it only works on the windows platform, it’s easier to use and has a lot more flexibility than the XmlSerializer. Introduction to the IntermediateSerializer can be found here, and a more extensive explanation can be found here. Following that, you might want to check out how to write your own ContentTypeSerializer, and specifically, how to do it for a collection of shared resources.
As you have seen in the example about collections of shared resources, Shawn only deals with the List class, leaving us to work through the rest. Well, here is a Dictionary class with the values as shared resources.
Dictionary evolves to… SharedResourceDictionary!
The shared resource dictionary is nothing more than an empty wrapper:
This is where the serializing gets done. As a side note, you don’t need to do anything special to get the SharedResourceDictionary to use the SharedResourceDictionarySerializer, just by having both classes in your project it’s fine.
Sorry about the “strung out” format of the code, otherwise the blog settings compact it into an unreadable mess.
Calling the serializer
To use this code, we have a dictionary we want to serialize:
To serialize it to Xml we do this:
To deserialize from Xml we do this:
Special thanks to r2d2rigo for helping me hunt down some bugs in the code.