Reputation: 84
I am trying to generate c# code from an XSD file and the enumerations are prepended with the word Item. How to I make this so that the enumerations are just numbers? The point of all this is to have an enumeration that only numbers can be used without restricting the type using Min/Max value. We don't want to restrict it like that because we distribute this XSD to a client and the comments in the XSD are needed. We also like having the enumerations in the c# code. We could manually edit the generated code but I'm not crazy about that if we can fix it in the xsd. Any ideas?
The relevant XSD schema
<xs:simpleType name="DriverActionsCode">
<xs:restriction base="xs:token">
<xs:enumeration value="00" />
<!--No Action-->
<xs:enumeration value="01" />
<!--Exceeded Safe/Posted Speed Limit-->
<xs:enumeration value="02" />
<!--Impeded Traffic-->
<xs:enumeration value="03" />
<!--Failed to Yield ROW-->
<xs:enumeration value="04" />
<!--Disregarded Stop Sign-->
<xs:enumeration value="05" />
<!--Failed to Stop at Signal-->
<xs:enumeration value="06" />
<!--Disregarded Other Device-->
<xs:enumeration value="07" />
<!--Improper Turn-->
<xs:enumeration value="08" />
<!--Turned from Wrong Lane or Position-->
<xs:enumeration value="09" />
<!--Other Improper Turns-->
<xs:enumeration value="10" />
<!--Lane Violation-->
<xs:enumeration value="11" />
<!--Improper Passing on Left-->
<xs:enumeration value="12" />
<!--Improper Passing on Right-->
<xs:enumeration value="13" />
<!--Followed Too Closely-->
<xs:enumeration value="14" />
<!--Improper Backing-->
<xs:enumeration value="15" />
<!--Signaling Violation-->
<xs:enumeration value="16" />
<!--Reckless Driving-->
<xs:enumeration value="17" />
<!--Careless Driving-->
</xs:restriction>
</xs:simpleType>
The generated class
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")]
[System.SerializableAttribute()]
public enum DriverActionsCode
{
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("00")]
Item00,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("01")]
Item01,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("02")]
Item02,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("03")]
Item03,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("04")]
Item04,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("05")]
Item05,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("06")]
Item06,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("07")]
Item07,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("08")]
Item08,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("09")]
Item09,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("10")]
Item10,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("11")]
Item11,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("12")]
Item12,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("13")]
Item13,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("14")]
Item14,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("15")]
Item15,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("16")]
Item16,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("17")]
Item17,
}
Upvotes: 0
Views: 595
Reputation: 52788
You can't, xsd.exe doesn't really support these sort of customisations. And even if you could, it would not be valid C#.
For example, this enum won't compile:
enum DriverActionsCode
{
00,
01,
//etc.
}
Identifiers must start with a character or underscore. From 2.4.2 of the C# spec:
identifier-start-character:
letter-character
_ (the underscore character U+005F)
However, if your numbering starts from 0
and has no gaps (as in the example above), you can use integers in your code and cast it to the enum.
For example:
Int32 codeInApplication = 10;
DriverActionsCode codeForXml = (DriverActionsCode)codeInApplication;
//DriverActionsCode.Item10;
Upvotes: 2