DataContractSerializer fails, null data
Asked Answered
P

1

6

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?

Podophyllin answered 23/12, 2012 at 20:18 Comment(3)
I'd start with comparing the contents of the data object between the two first. Are they the same?Strangulate
The data object are exactly the same. That's what's got me so befuddled :(Podophyllin
The data in both cases is XML, both cases seem to carry the data all the way until that one line as I highlighted.Podophyllin
Z
13

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:

  1. ̶a̶l̶p̶h̶a̶b̶e̶t̶i̶c̶a̶l̶l̶y̶ ̶o̶r̶d̶e̶r̶ ̶y̶o̶u̶ ̶X̶M̶L̶. No, that's really not an option.
  2. Since it seems like you're getting RSS, you can consider using WCF's System.ServiceModel.Syndication.SyndicationFeed which is specfically built for RSS/Atom. More on this at this great article by Den Delimarsky.
  3. If your using non-RSS XML, consider using straight up Xml Serialization with XmlSerializer. For my money it's the best and simplest way to do over the wire XML serialization and deserialization.
  4. The behaviour change from WP7 to WP8 is enabled via Quirks Mode. Meaning that WP7 apps running on WP8 will still run OK. If your app doesn't benefit from WP8 featuresets, you can keep your entire app as a WP7 app.
Zalea answered 23/12, 2012 at 22:27 Comment(1)
Omg. Well, this is going to be difficult :/ Thanks!Podophyllin

© 2022 - 2024 — McMap. All rights reserved.