I've got some code for Windows Phone 7, for a RSS app:
private RSSSettings DeserializeSettings(string data)
{
RSSSettings rsssettings;
try
{
var ser = new DataContractSerializer(typeof(RSSSettings));
using (var sr = new StringReader(data))
using (var xr = XmlReader.Create(sr))
rsssettings = (RSSSettings)ser.ReadObject(xr);
}
catch (Exception ex)
{
ex.ToString();
rsssettings = new RSSSettings() { Version = -1 };
}
return rsssettings;
}
It works perfectly on Windows Phone 7. I ported the app to Windows Phone 8, and everything else in the app works except for this snippet.
Comparing what happens in Windows Phone 7 and 8, "rsssettings" in WP8 remains null while it populates correctly in WP7. There have been no code changes to this portion of the code at all.
Everything works the same until:
rsssettings = (RSSSettings)ser.ReadObject(xr);
The exception is not called.
Anyone have a clue as to how to resolve this frustrating issue?
Yeah, you're not going to like this.
DataContractSerializer on WP8 requires XML elements be organized alphabetically. That's done since the assumption that everything that goes into DataContractSerializer comes out of another DataContractSerializer.
I wish I was making this up, but I'm not. From MSDN's App platform compatibility for Windows Phone:
DataContractSerializer class
WP8 Behaviour: XML elements must be sorted alphabetically.
WP7 Behaviour: XML elements can be unordered.
There are a few alternatives for you at this point: