WSDL生成正确
问题描述:
我已经通过我们的供应商之一,给予了WSDL包含下面的代码不反序列化长XML:WSDL生成正确
<xsd:element name="RegistrationResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="DateTimeStamp" type="xsd:dateTime" nillable="true"/>
<xsd:element name="EchoData" type="xsd:string" nillable="true"/>
<xsd:element name="TransactionTrace" type="xsd:long" nillable="true"/>
<xsd:element name="ResponseCode" type="xsd:int" nillable="true"/>
<xsd:element name="ResponseMessage" type="xsd:string" nillable="true"/>
<xsd:element name="ClientAccNumber" type="xsd:long" nillable="true"/>
<xsd:element name="BranchCode" type="xsd:int" nillable="true"/>
<xsd:element name="HIN" type="xsd:long" nillable="true"/>
<xsd:element name="EasyPayRef" type="xsd:long" nillable="true"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
但是有时候响应我得到他们的回复将不包含所有字段。例如,在这种情况下:
<soapenv:Body>
<tpw:RegistrationResponse>
<DateTimeStamp>
2012-04-02T19:10:41.4430564Z
</DateTimeStamp>
<EchoData/>
<TransactionTrace>
5418721751027669946
</TransactionTrace>
<ResponseCode>
25
</ResponseCode>
<ResponseMessage>
Invalid Mobile Account Type
</ResponseMessage>
<ClientAccNumber/>
<BranchCode/>
<HIN>
0
</HIN>
<EasyPayRef/>
</tpw:RegistrationResponse>
</soapenv:Body>
现在添加服务引用时在Visual Studio中的代码生成的代码不喜欢的事实,ClientAccNumber是空白。生成的代码如下:
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tpwebservice.x.com", Order=5)]
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public System.Nullable<long> ClientAccNumber;
我得到一个“输入不正确的格式”异常,当它试图反序列化从服务器接收到的响应。我在想的是,它看到一个空白字符串,并试图解析一长串,这显然失败了。我试图将minOccurs =“0”添加到wsdl,但没有帮助。
如何解决wsdl或生成的代码来解决此问题?还是有什么我失踪?
答
我会改变的代码来定义属性作为字符串,并有一个非XML序列化的特性与实际Nullable<long>
值被转换到/从字符串:
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tpwebservice.x.com", Order=5)]
[System.Xml.Serialization.XmlElementAttribute("ClientAccNumber", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public string ClientAccNumberStr;
[System.Xml.Serialization.XmlIgnoreAttribute]
public System.Nullable<long> ClientAccNumber {
get {
if (string.IsNullOrEmpty(ClientAccNumberStr))
return null;
return long.Parse(ClientAccNumberStr);
}
set {
if (!value.HasValue) {
ClientAccNumberStr = null;
} else {
ClientAccNumberStr = value.Value.ToString();
}
}
}
但为了添加额外的属性,我需要编辑生成的代码,这成为一个噩梦,因为每次WSDL重新生成时,更改都会丢失。 – Dylan 2012-04-02 20:55:56
您的WSDL与您获得的XML不匹配 - 您将在每次修改WSDL或修复代码时修复WSDL,但我没有看到解决方法。 – MiMo 2012-04-02 21:38:02
或者要求供应商发送一个零而不是空的元素?或者你可以以某种方式预处理他们的XML并纠正它是必要的? – davidfrancis 2012-04-02 21:40:44