Serializing to Binary on WP7 - Part 1
Serialising data out to a file on WP7 is a very easy task thanks to the DataContractSerializer which allows you to tag properties in your class with '[DataMember]' and then use a few simple line of code to save/load that class to file:
1: // Save
2: MyClass theData = new MyClass();
3: using (IsolatedStorageFileStream fs = isoStore.OpenFile("MyClass.xml", FileMode.Create))
4: {
5: DataContractSerializer ser = new DataContractSerializer(typeof(MyClass));
6: ser.WriteObject(fs, theData);
7: }
8:
9: // Load
10: using (IsolatedStorageFileStream fs = isoStore.OpenFile("MyClass.xml", FileMode.Open))
11: {
12: DataContractSerializer ser = new DataContractSerializer(typeof(MyClass));
13: MyClass result = (MyClass)ser.ReadObject(fs);
14: }
This is all well and good for getting something up and running quickly, but once you start having more complex classes and/or more data, performance starts to become an issue. A quick test of how long AppTracker currently takes to deserialize the cache file when following just 3 apps (Polyglot, Gifts and AppTracker - consisting of a total of about 544 reviews) resulted in the following timings:
Thursday, May 19, 2011 at 9:15AM