Link to home
Start Free TrialLog in
Avatar of nicholsf
nicholsfFlag for United States of America

asked on

XSD how to specify null in enum

For XSD definition, how can null be included as enum for a field defined as string?

Is this correct:     <xs:enumeration value="null"/>

    <xs:element name="status_color" nillable="true">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="G"/>
                <xs:enumeration value="R"/>
                <xs:enumeration value="Y"/>
                <xs:enumeration value="null"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

I'm using xsd.exe /xsd2code.exe and it   turns the code into this: @null

  [System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.3.0.31927")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://pmve.web.boeing.com/action_item_dtl_in")]
    public enum status_color {
        G,
        R,
        Y,
        @null,
    }
   
Avatar of kaufmed
kaufmed
Flag of United States of America image

Enums in C# are value types--hence they cannot take a value of null. As far as the "@null", "null" is a reserved word in C#. Because of this, the "@" was pre-pended to it. You can use any reserved word as an identifier in C# if you preface it with an "at" symbol.

E.g.

   @int
   @struct
   @class
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nicholsf

ASKER

What about xsd and is this valid for defining null:

 <xs:enumeration value="null"/>

I think it is but I'm not a 100%

Note: I realize that  nillable="true" for the element will work. but if you want a list of valid enum values.
I don't have access to my copy of XMLSpy ATM; the best I can offer right now would be to try empty string for nil:
<xs:enumeration value=""/>

Open in new window

It seems to me, however, that having a value of nil defeats the purpose of having an enumeration. An enumeration is a way of saying: "This item MUST be one of these values." A value of nil seems counter-intuitive to me.
Many thanks for providing the info on the @ in C#.
That was helpful..increased my knowledge of C#.