UWP XmlSerializer PlatformNotSupportedException

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 01:14:55

问题


I'm using XmlSerializer in a UWP project. It works fine when compiled for debug but throws PlatformNotSupportedException in release.

I've written a simple example C# program to illustrate this. I have not edited any project settings from what VS 2017 has given me. I have a simple SampleData.xml file in the project as Content. I have classes called SampleItemsForSerialization along with SampleItemList and SampleItem, all marked up to be able to serialize the xml file.

In another class I have the following code:

try
{
    using (Stream strmRead = await GetAFileStreamForRead(_strFILENAME))
    {
        Type typeSampleItems = typeof(SampleItemsForSerialization);
        XmlSerializer xmlSerializer = new XmlSerializer(typeSampleItems, new XmlRootAttribute("MyRoot"));

        _sampleitems = (SampleItemsForSerialization)xmlSerializer.Deserialize(strmRead);

        bLoadedOK = true;
}
catch (Exception ex)
{ ... }

When compiled debug the app works fine, the xml file is deserialized, and I can then show the content of the file in a message dialog.

When compiled for release, the XmlSerializer constructor throws that PlatformNotSupportedException. Searching on this problem has yielded all sorts of talk and supposed solutions that often conflict with one another. My favorite is the build property GenerateSerializationAssemblies. Some say it should be On, others say Off, or maybe Auto, I don't know?

Can anyone make this one simple thing work?

ETA - Anyone wanting to take a crack at this should be able to recreate the problem on their own system:

  • Start a new "Blank App (Universal Windows)" project in VS 2017.
  • Create an XML file with some simple data.
  • Create classes to support your xml file marked appropriately to be supported by XmlSerializer.
  • In the MainPage.xaml that was created for you add a button and then a click handler for that button.
  • In the click handler use code similar to what I posted above to load your xml into the object of the type representing the root element of your xml file. Use a MessageDialog or something to display the data.
  • Compile debug and correct any typos, etc. Get it until the app runs just fine in debug with no errors, displaying the expected data.
  • Now change to compile in release mode. Run the app again and you should get the exception.
  • 来源:https://stackoverflow.com/questions/46620047/uwp-xmlserializer-platformnotsupportedexception

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!