Click here to Skip to main content
Click here to Skip to main content

Tagged as

Problems with XML Serialization and .NET 4.5

, 19 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Installing .NET 4.5 breaks serialization to XML

Introduction

This is just a quick tip to help people avoid issues with XML serialization in .NET 4.5, particularly with applications written in 4.0 or earlier versions.

Background

I spent a number of days trying to figure out why our application written in .NET 4.0 can't serialize CLR objects to XML if .NET Framework 4.5 is installed. Backwards compatibility has never been an issue for me in .NET which is why I found this particular problem quite so frustrating. Given that frustration, if this tip helps only one other person avoid this problem I'll be a very happy man.

I kept on getting the following exception when calling XmlSerializer.Serialize:

 System.InvalidOperationException: There was an error generating the XML document. 
   ---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterFrmFrm.Write53_FrmTable(
      String n, String ns, FrmTable o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterFrmFrm.Write55_FrmFrm(
      String n, String ns, FrmFrm o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterFrmFrm.Write56_frm(Object o)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, 
      XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o)

Messing around with version settings didn't help one bit, e.g.:

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />

Neither did changing the project settings:

I eventually managed to formulate a search that found an answered question on StackOverflow:

Which pointed me in the right direction. I was certain that it was something to do with the .NET versions, but how to configure it so that it actually works?

Using the code

All you need to do is to tell the CLR in your .config file to use the legacy XmlSerializer that isn't part of version 4.5. For this there is a dedicated configuration section:

<configuration>
  <system.xml.serialization>
    <xmlSerializer useLegacySerializerGeneration="true"/>
  </system.xml.serialization>
</configuration>

In the process I learnt a bit about how the different .NET major and minor versions relate to each other. 4 and 4.5 are basically just CLR 4. Once you install 4.5 the CLR assumes you want to use the latest and greatest XmlSerializer instead of the one that ships with 4.0, so you have to explicitly tell the CLR which one to use.

History

  • 19.06.2013 - First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jim lahey
Software Developer (Senior)
United Kingdom United Kingdom
First batch file in 1987
 
Messed around with the Bullfrog C++ Libraries for Syndicate in 1994
 
Web Developer since 2000
 
.net Developer since 2001
 
MCTS: Microsoft® .NET Framework 2.0 - Web-based Client Development
 
MCTS: Web Applications Development with Microsoft .NET Framework 4

Comments and Discussions

 
QuestionMy Vote of 5 Pinmemberqwer241354123426-Jan-14 21:35 
GeneralMy vote of 5 Pinmemberjohannesnestler21-Jun-13 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 19 Jun 2013
Article Copyright 2013 by jim lahey
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid