开发者

Using XML Enumerations with Delphi XML Data Binding Wizard

开发者 https://www.devze.com 2022-12-17 16:09 出处:网络
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 an

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:

  1. can contain characters incompatible with a Delphi identifier
  2. are case sensitive

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

0

精彩评论

暂无评论...
验证码 换一张
取 消