I have an XSD where I define an enumeration specifying numerical values for each element. It looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://mariusbancila.ro/demo/1.0" xmlns:ser="http://schemas.microsoft.com/2003/10/Serialization/" elementFormDefault="qualified" targetNamespace="http://mariusbancila.ro/demo/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="statusCode">
<xs:restriction base="xs:string">
<xs:enumeration value="none"/>
<xs:enumeration value="alpha">
<xs:annotation>
<xs:appinfo>
<EnumerationValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/">100</EnumerationValue>
</xs:appinfo>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="beta">
<xs:annotation>
<xs:appinfo>
<EnumerationValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/">200</EnumerationValue>
</xs:appinfo>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="gamma">
<xs:annotation>
<xs:appinfo>
<EnumerationValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/">300</EnumerationValue>
</xs:appinfo>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:element name="demo">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="status" type="tns:statusCode"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I use xsd.exe to generate C# classes from this (xsd.exe /namespace:Demo /language:cs /c demo.xsd). The result is as follows:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mariusbancila.ro/demo/1.0")]
public enum statusCode {
/// <remarks/>
none,
/// <remarks/>
alpha,
/// <remarks/>
beta,
/// <remarks/>
gamma,
}
My expectation is that I get the following:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mariusbancila.ro/demo/1.0")]
public enum statusCode {
/// <remarks/>
none,
/// <remarks/>
alpha = 100,
/// <remarks/>
beta = 200,
/// <remarks/>
gamma = 300,
}
My problem with this is that the numerical values of these enums have been stored in a database. In a next version, the enum in the XSD has changed, with new enum values generated by xsd.exe in between the previous ones. For example:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mariusbancila.ro/demo/2.0")]
public enum statusCode {
/// <remarks/>
none,
/// <remarks/>
alpha,
/// <remarks/>
beta,
/// <remarks/>
delta, // this is now 3
/// <remarks/>
gamma, // this used to be 3, now it's 4
}
And this messed up the handling of the stored numerical values in other parts of the system.
Therefore, I'm looking for a way to generate the C# code with the numerical values. I know I can manually edit the result, but that is error-prone and requires manual intervention each time the XSD changes, which is something I definitely want to avoid.
I wouldn't recommend you to use those objects directly in your code. You need to create a Façade and then use it as bridge between your code and the ugly outside world.
Additionally to the Façade, one thing you could do is to create an enum class for you and inside of it you write a operator to convert from the enum provided in the xsd to your enum class. And, in your code, you use only your enum class.
Combining the façade and the enum class your code is unaware of changes in the xsd schema.