Jared.Greiner on Wed, 21 Dec 2011 21:35:03
SharpSerializer is an excellent way to save data in XML or binary formats. I use it in my games to serialize custom objects with references to other custom objects, something that is difficult to accomplish with the standard XML Serializer. I stumbled upon it after reading this article comparing methods of serialization on Windows Phone 7.
The silverlight version of SharpSerializer works on Window's phone, but it is not designed to work with the XNA framework. Specifically it will return Vector2's and many other variables as blank (0,0) upon deserialization. After deciphering this page in German, I understood how to extend SharpSerializer to treat Vector2's and other XNA classes as "Simple Types".
In the Core/Tools.cs class I extended the "IsSimple" method as shown below:
In the Advanced/SimpleValueConverter.cs class I extended the "ConvertFromString" method as shown below:
Both the deserialization of Vector2 and rectangle work by turning the XML string value into a char array, then iterating through the array and assembling new separate strings to represent the individual values of the type. These values are then parsed into ints or floats as needed. The Vector2 deserialization can handle a Vector2 with two floats as well as one with two ints. It properly handles values with several digets. The rectangle deserialization requres all ints.
I'm open to suggestions on how to improve my code. I'm really a novice programmer with no formal education in computer science. I'll also check back to answer questions, so throw them my way.
sir pawian on Fri, 27 Apr 2012 06:12:38
hi Jared, on the following page you can compare your results with another solution which also works.
it's german page but c# example is english ;-)