Question

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?

Était-ce utile?

La solution

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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top