【发布时间】:2013-01-24 15:27:00
【问题描述】:
好的,显然我在这里做错了。我正在尝试创建一个 Web 服务,并且我希望“dateShipped”是可选的,这意味着在 WSDL 中,我希望 minOccurs="0"
[Serializable]
[XmlType]
public class CTShipment
{
[XmlElement(Order = 0, IsNullable=false)] public CTDeliveryMethod DeliveryMethod;
[XmlElement(Order = 1, IsNullable=false)] public CTShipmentAddress ShipmentAddress;
[XmlIgnore] public bool dateShippedSpecified;
[XmlElement(Order = 2, IsNullable=false)] public DateTime dateShipped;
}
我希望像这样生成 WSDL:
<xs:complexType name="CTShipment">
<xs:annotation>
<xs:documentation>All details for the shipment of a suborder.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="DeliveryMethod" type="CTDeliveryMethod" nillable="false"/>
<xs:element name="ShipmentAddress" type="CTShipmentAddress" nillable="false"/>
<xs:element name="dateShipped" type="xs:dateTime" nillable="false" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
实际上我得到的是这样的:
<xs:complexType name="CTShipment">
<xs:sequence>
<xs:element name="DeliveryMethod" nillable="true" type="tns:CTDeliveryMethod"/>
<xs:element name="ShipmentAddress" nillable="true" type="tns:CTShipmentAddress"/>
<xs:element name="dateShipped" type="xs:dateTime"/>
<xs:element name="dateShippedSpecified" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
根据我读过的几件事(包括http://msdn.microsoft.com/en-us/library/zds0b35c%28v=vs.90%29.aspx),包括公共布尔“dateShippedSpecified”应该使“dateShipped”可选(minOccurs=0)。如您所见,这不仅没有发生,而且“dateShippedSpecified”显示在 WSDL 中,即使它被标记为“[XmlIgnore]”。您可能已经注意到还有另一个问题:即使我指定了“IsNullable=false”,我仍然在 WSDL 中得到 nillable="true"。
这至少有 4 个问题我无法解释所有与同一事物相关的问题:
-
如何在我的 WSDL 中将 minOccurs 设置为 0?
-
为什么 [fieldName] 指定的模式不能使 [fieldName] 成为可选 (minOccurs = 0)?
-
即使它没有遵循 ___Specified 模式,如果 dateShippedSpecified 标记为 XmlIgnore,为什么它会显示在 WSDL 中?
-
即使我指定“IsNullable=false”,为什么所有内容都标记为 nillable="true"?
还有一个额外的问题,如果有人知道...
-
如何让注解(如下图)被包含进来?
<xs:annotation> <xs:documentation>All details for the shipment of a suborder.</xs:documentation> </xs:annotation>
【问题讨论】:
-
作为记录,我已经离开了 XML 序列化器,现在正在使用数据协定序列化器。我可以设置 minOccurs 以便我能够继续我的项目,但我仍然对 XML 序列化程序的情况感到好奇,如果有人知道的话。
标签: c# web-services soap wsdl