0

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.

user1039513
  • 812
  • 6
  • 5
  • Try turning on the 'generate serialization assemblies' on your project - does that have the same issue? – Ian Jun 19 '13 at 11:22
  • I've tried setting this to all available options on the project itself, and the .exe that hosts the component. Nothing changes. It doesn't do anything to the stacktrace either. – user1039513 Jun 19 '13 at 12:12
  • Try running the exe as an administrator, could be a permissions issue. – Chris Searles Jun 19 '13 at 12:38
  • @Ian: With all due respect, you didn't read the post properly. Stack trace includes the innerException. The code snippet is the bit where it breaks, not all the code round it. Besides which, I think we've fixed the issue: http://stackoverflow.com/questions/14689305/serialization-breaks-in-net-4-5 – user1039513 Jun 19 '13 at 12:38
  • @Chris Searles: Our customers often run in restricted accounts, so this isn't an option. Besides which, shouldn't I be seeing some sort of security related exception in the stacktrace? – user1039513 Jun 19 '13 at 12:40
  • Not saying it's your ultimate solution but might give you a starting point from which to solve the problem. I've seen some weird issues lately with .NET and administrative permissions so I wouldn't rule it out without trying it. – Chris Searles Jun 19 '13 at 12:46
  • @Chris Searles: think I fixed it, http://stackoverflow.com/questions/14689305/serialization-breaks-in-net-4-5, just getting a few others to verify it before I close the question. Thanks for your help though :) – user1039513 Jun 19 '13 at 12:54
  • Question answered here: http://stackoverflow.com/questions/14689305/serialization-breaks-in-net-4-5 – user1039513 Jun 19 '13 at 13:52

0 Answers0