0

I have a strange problem with XMLSerializer.Serialize - when serializing an object with a valid enum valie (e.g. Item911), I receive an InvalidOperationException with error message "There was an error generating the XML document."

But: The enum value seems valid, it is in the generated enum from the contract (enum has been created automatically via xsd.exe)

The stack trace points me to the call of System.Xml.Serialization.XmlSerializer.Serialize, the rest is generated code from microsoft:

<Exception
  Type=System.InvalidOperationException
  Message=Instance validation error: '911' is not a valid value for YYY.
  Source=Microsoft.GeneratedCode>
  <StackTrace><![CDATA[    
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSonstigeFolgeeingabe.Write61_DienststelleMenge(DienststelleMenge v)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSonstigeFolgeeingabe.Write65_Aktenzeichen(String n, String ns, Aktenzeichen o, Boolean isNullable, Boolean needType)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSonstigeFolgeeingabe.Write140_SonstigeFolgeeingabe(String n, String ns, SonstigeFolgeeingabe o, Boolean isNullable, Boolean needType)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSonstigeFolgeeingabe.Write141_SonstigeFolgeeingabe(Object o)
  ]]>
  </StackTrace>
</Exception>

It only occurs in production (release compiled), when I try to debug it the code (even with Data where it fails in production), it works.

Additional context:

  • the data is for a payload of a webservice call
  • the classes are generated via xsd.exe from xsd-files and not manipulated
  • I wasn't able to reproduce the bug

I tried to reproduce the bug locally and at the customer (working in a debug environment with mocked webservices), but I wasn't able to reproduce the bug.

  • You don't share a [mcve] so it's hard to say for sure what your problem is, but `XmlSerializer` does not support serializing & deserializing numeric values as enums. Instead it only supports serializing & deserializing enum **names**. If you want to deserialize the value, see [How do I serialize an enum value as an int?](https://stackoverflow.com/q/506368). But to give a definite answer we will need likely need more details, ideally a [mcve], specifically a c# data model and XML that generate the exception. – dbc Mar 27 '23 at 15:23
  • Or is the exception getting thrown from with the the `XmlSerializer` constructor? – dbc Mar 27 '23 at 15:25
  • I made progress - I found a blog post [here](https://www.hanselman.com/blog/how-to-debug-into-a-net-xmlserializer-generated-assembly) about how to generate/find the generated assemblies used for serialization. (In another post [here](https://www.hanselman.com/blog/changing-where-xmlserializer-outputs-temporary-assemblies) is also a way to configure the path of these files ) The production environment is currently configured that every 24h the windows service is restarted - so maybe the temporary assembly is the regenerated and the error no longer occurres? – DarkHelmet Jul 13 '23 at 14:14

0 Answers0