我们的供应商之一给了我一个包含以下内容的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或生成的代码来解决此问题?或者还有其他我想念的东西?
答案 0 :(得分:0)
我更改代码以将属性定义为字符串,并且具有非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();
}
}
}