I have an XML schema that uses enumerations, but when I look at the generated XML object in Delphi, the enumeration restriction has been dropped. Is there any way to get Delphi to generate the enum and build it into the object?
XSD Snippet:
<xs:simpleType name="enumType" final="restriction">
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="Each"/>
<xs:enumeration value="Units"/>
<xs:enumeration value="Area"/>
<xs:enumeration value="Payroll"/>
<xs:enumeration value="Sales"/>
<xs:enumeration value="TotalCost"/>
<xs:enumeration value="Other"/>
</xs:restriction>
</xs:simpleType>
What I would expect to see in Delphi is a field that accepts a enum that is then converted to it's corresponing string when the XML is generated, but that field is just an ordinary string.
What you can do is create your own enumerated type with the same string constants as names and use the unit TypInfo with the function GetEnumValue and GetEnumString. This allows you to prefix the names with a few lowercase letters like in other Delphi code:
Value := TMyEnum( GetEnumValue( typeinfo( TMyEnum ), Prefix + AString ) )
It is not possible for the XML data binding wizard to do what you want.
The reason is that enumerations in an XSD are not compatible with delphi identifiers because they:
Basically XSD enumerations are just strings with a constrained of values.
See the enumeration specs and an example.
Both are clearly incompatible with Delphi enumeration types.
Edit: 20100125 - Delphi attributes
Here is an interesting question on how far you could go with the new attribute and RTTI support in Delphi 2010.
--jeroen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With