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

随声附和 提交于 2019-12-19 18:27:10

问题


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 have 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>

回答1:


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>


来源:https://stackoverflow.com/questions/3600818/wsdl-enums-and-c-its-still-murky

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!