We've got a WinForms application mainly written in 4.0 that is having major problems serializing C# objects to Xml. The component that does the serialization is 3.5 but that shouldn't be an issue.
Running in debug mode using Visual Studio is fine, but as soon as we run the application from the .exe we get the following error when we serialize to Xml:
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)
I've tried a number of different ways of serializing to Xml and nothing seems to change the outcome, XmlSerializer.Serialize calls always break. Here's our particular call:
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XDocument xmlDocument = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
using (XmlWriter documentWriter = xmlDocument.CreateWriter())
{
serializer.Serialize(documentWriter, obj);
}
return xmlDocument;
It's worth noting that we only get this problem on machines with .net 4.5 installed. If I take that off and put 4.0 on it works fine. I've also tried to force the .net version in app.config:
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
But also to no avail.
If anyone can help with this issue I'd be extremely grateful.