开发者

WSDL, Enums and C#: It's still murky

开发者 https://www.devze.com 2023-01-13 12:28 出处:网络
I tried to look this up online, but all the WSDL examples seem to not really explain if I should mark things as basetype string in the WSDL or int...

I tried to look this up online, but all the WSDL examples seem to not really explain if I should mark things as basetype string in the WSDL or int...

Basically, I'm trying to make my WSDL so that I can represent an Enumeration. I ha开发者_如何学编程ve a C# Enum in mind already that I want to match it up to...

public enum MyEnum {
    Item1 = 0,
    Item2 = 1,
    Item3 = 2,
    SpecialItem = 99
}

I'm not sure how my WSDL should look... I figure it's one of two, but even then I'm not 100% sure...

<wsdl:types>
    <xsd:schema targetNamespace="http://www.mysite.com/MyApp"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                >
        <xsd:simpleType name="MyEnum">
            <xsd:restriction base="xsd:int">
                <xsd:enumeration value="0" />
                <xsd:enumeration value="1" />
                <xsd:enumeration value="2" />
                <xsd:enumeration value="99" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:schema>
</wsdl:types>

OR

<wsdl:types>
    <xsd:schema targetNamespace="http://www.mysite.com/MyApp"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                >
        <xsd:simpleType name="MyEnum">
            <xsd:restriction base="xsd:string">
                <xsd:enumeration value="Item1" />
                <xsd:enumeration value="Item2" />
                <xsd:enumeration value="Item3" />
                <xsd:enumeration value="SpecialItem" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:schema>
</wsdl:types>


Enumerations will end up looking like their string representations. So the correct wsdl will present the enums as:

<xs:simpleType name="MyEnum">
    <xs:restriction base="xsd:string">
      <xs:enumeration value="Item1" />
      <xs:enumeration value="Item2" />
      <xs:enumeration value="Item3" />
      <xs:enumeration value="SpecialItem" />
    </xs:restriction>
  </xs:simpleType>

The above will automatically serialize/deserialize to the MyEnum enumeration type for you. If you present the enums as xsd:int then you will end up having to convert them manually back and forth.

You can refer to the enumeration definition within your schema like so:

<xsd:complexType name="Class1">
    <xsd:sequence>
      <xsd:element minOccurs="1" maxOccurs="1" name="MyEnumProperty" type="MyEnum" />
    </xsd:sequence>
  </xsd:complexType>
0

精彩评论

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