user5762768
user5762768

Reputation: 11

How to bypass enum validation when using XML (de)Serialization

For handling XML in C# where using .NET System.Xml.Serialization.

Starting point is an XSD like:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
  <xsd:element name="Demo" type="DemoType"/>
  <xsd:complexType name="DemoType">
    <xsd:sequence>
      <xsd:element name="Some" type="SomeType" minOccurs="3" maxOccurs="6" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="SomeType">
    <xsd:sequence>
      <xsd:element name="enumValue" type="enumValueType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="enumValueType">
    <xsd:attribute name="type" use="required">
      <xsd:simpleType>
        <xsd:restriction base="xsd:string">
          <xsd:enumeration value="Value_1"/>
          <xsd:enumeration value="Value_2"/>
        </xsd:restriction>
      </xsd:simpleType>
    </xsd:attribute>
  </xsd:complexType>
</xsd:schema>

Next, we're using xsd.exe to generate classes from the XSD, resulting in

namespace myNamespace {

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("Demo", Namespace="", IsNullable=false)]
public partial class DemoType {

    private SomeType[] someField;

    [System.Xml.Serialization.XmlElementAttribute("Some", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public SomeType[] Some {
        get {
            return this.someField;
        }
        set {
            this.someField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class SomeType {

    private enumValueType enumValueField;

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public enumValueType enumValue {
        get {
            return this.enumValueField;
        }
        set {
            this.enumValueField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class enumValueType {

    private enumValueTypeType typeField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public enumValueTypeType type {
        get {
            return this.typeField;
        }
        set {
            this.typeField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public enum enumValueTypeType {

    Value_1,

    Value_2,
}

}

Unfortunately, it turns out that the XML passed to the application is not always well-formed. Next to the defined values 'Value_1' and 'Value_2' we also find situations having an enumValue of type 'Value_3'. As a result, (de)serialization of the XML using the DemoType class fails.

Is there a way to have the XML (de)serialized without changing the original XSD? The XSD is very large and provided by a vendor. Also changing the generated classes is not preferred because the changes will get lost when regenerating the file.

Upvotes: 1

Views: 445

Answers (1)

Cleptus
Cleptus

Reputation: 3541

You could, should? use schema versioning, to make sure all XML fit to the schema they were created, have a look at this related question. What are the best practices for versioning XML schemas?

Upvotes: 0

Related Questions