title: How to ignore invalid enum values during xml deserialization?
body: <p>I want to deserialize an xml document to a class, which is genereated by the concerning xsd files. I don't have control over the contents of the xml-file.</p>
  
  <p>During deserialization I run into an exception, because an enum value in the xml document does not meet the requiremnts of the xsd. Instead of breaking, i would like the deserialization to continue and just take the default value for any such errors. Is there any way to accomplish this behaviour?</p>
  
  <hr>
  
  <p>edit:
  For clarification, what i am trying to achieve: I want to read data from digital invoices. So the creation of the xml file is some kind of blackbox and can contain possibly flase values, even if the structure meets the standards. But that does not mean, that every value is flawed in that way. The exception prevents me from reading the correct values so i just want the deserialization to finish by somehow inserting the default values if such an error occurs.</p>
  
  <p>Neither marking the values as obsolete, nor flagging them with XmlIgnore won't work, because the next xml i receive could contain correct values.</p>
  
  <p>I hope that helped clarifying the problem.</p>
  
  <hr>
  
  <p>Right now, im using the <code>System.Xml.Serialization</code> dll, but im willing to implement any library which can help me achieve the wanted behaviour.</p>
  
  <p>The exception im getting:</p>
  
  <blockquote>
    <p>"System.InvalidOperationException: Instance validation error: 'x' is
    not a valid value for xType.."</p>
  </blockquote>
  
  <p>The code that throws the exception:</p>
  
  <pre><code>XmlSerializer serializer = new xml.XmlSerializer(typeof(MyType));
  MyType invoice = serializer.Deserialize(memoryStream) as MyType;
  </code></pre>
  
  <p>I know the code does not help very much, so I'll add the enum, that is currently problematic:</p>
  
  <pre><code>public enum PaymentMeansCodeContentType
      {
  
          [System.Xml.Serialization.XmlEnumAttribute("10")]
          Item10,
  
          [System.Xml.Serialization.XmlEnumAttribute("20")]
          Item20,
  
          [System.Xml.Serialization.XmlEnumAttribute("30")]
          Item30,
  
          [System.Xml.Serialization.XmlEnumAttribute("48")]
          Item48,
  
          [System.Xml.Serialization.XmlEnumAttribute("49")]
          Item49,
  
          [System.Xml.Serialization.XmlEnumAttribute("57")]
          Item57,
  
          [System.Xml.Serialization.XmlEnumAttribute("58")]
          Item58,
  
          [System.Xml.Serialization.XmlEnumAttribute("59")]
          Item59,
  
          ZZZ,
      }
  </code></pre>
  
  <p>These are autogenerated from using the xsd command line tool:
  <a href="https://docs.microsoft.com/de-de/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe" rel="nofollow noreferrer">https://docs.microsoft.com/de-de/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe</a></p>
  
  <p>The xml i need to deserialize provides me with a '1', so clearly an invalid value. Still i need to access the other valid values from the xml and provide means for indicating which values are flawed.</p>