2

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.
    • Delete the bin folder in the project which will force all the intermediate object to get rebuilt. There is something wrong with the dependencies of the source that a change isn't being recognized by the compiler and you project isn't getting fully recompiled. – jdweng Oct 07 '17 at 16:11
    • Thanks for the idea, @jdweng, but I already tried that on the original project that gave me this problem, plus I started a new test project just to demonstrate this problem and got the same behavior. – Tom in Dallas Oct 07 '17 at 17:54
    • 1
      Ok. Now we know what really happened and can get to the bottom of the issue. It looks like you tried to upgrade a project from a previous version of VS and the upgrade did not complete. The best solution is to edit the proj file which is text. I often have to do this because posted projects were built with different versions of VS than I have on my computer. So build a new project from scratch will no code in the project. Then compare the new proj file with one that does not work. Edit the versions numbers in the broken proj file to match the new working proj file. – jdweng Oct 07 '17 at 21:27
    • As far as I can tell, the problem lies with either (1) some project setting or reference I'm missing, or (2) a Microsoft bug. Either way, I can't afford any more time on this so I am Officially Giving Up. I've torn out all the XmlSerializer code and replaced it with XmlDocument/XmlElement/etc code. Frankly it's a better solution anyway. XmlDocument is a lot more flexible and forgiving, which is important when you consider that end users can edit (and make mistakes in) xml files. – Tom in Dallas Oct 10 '17 at 11:46

    0 Answers0