通过visual studio 11的“服务参考”,在c#中使用Web服务时,我遇到了一个非常烦人的问题。
我能够通过Web服务(在另一个域上)进行调用并获得响应,但在处理响应时会出现错误:
无法将类型为“System.Xml.XmlNode []”的对象强制转换为类型 ComplexType_Client_InformationResponse
这是wsdl:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="https://xxx.fr/client/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/" name="Service_Client" targetNamespace="https://xxx.fr/client/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema targetNamespace="https://xxx.fr/client/">
<xsd:complexType name="ComplexType_Client_InformationIn">
<xsd:all>
<xsd:element name="PARAM1" type="xsd:string" />
<xsd:element name="PARAM2" type="xsd:string" />
</xsd:all>
</xsd:complexType>
<xsd:complexType name="ComplexType_Client_InformationResponse">
<xsd:all>
<xsd:element name="ELEMENT1" type="xsd:int" />
<xsd:element name="ELEMENT2" nillable="true" type="xsd:string" />
<xsd:element name="ELEMENT3" nillable="true" type="xsd:string" />
<xsd:element name="ELEMENT4" nillable="true" type="xsd:string" />
<xsd:element name="ELEMENT5" nillable="true" type="xsd:string" />
</xsd:all>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="informationXIn">
<wsdl:part name="informationIn" type="tns:ComplexType_Client_InformationIn" />
</wsdl:message>
<wsdl:message name="informationXOut">
<wsdl:part name="return" type="tns:ComplexType_Client_InformationResponse" />
</wsdl:message>
<wsdl:portType name="Service_ClientPort">
<wsdl:operation name="informationX">
<documentation>@param ComplexType_Client_InformationIn $informationIn</documentation>
<wsdl:input message="tns:informationXIn" />
<wsdl:output message="tns:informationXOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Service_ClientBinding" type="tns:Service_ClientPort">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
<wsdl:operation name="informationX">
<soap:operation soapAction="https://xxx.fr/client/#informationX" />
<wsdl:input>
<soap:body use="encoded" namespace="https://xxx.fr/client/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:input>
<wsdl:output>
<soap:body use="encoded" namespace="https://xxx.fr/client/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service_ClientService">
<wsdl:port name="Service_ClientPort" binding="tns:Service_ClientBinding">
<soap:address location="https://xxx.fr/client/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
......和回复:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="https://xxx.fr/client"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:informationXResponse>
<return xsi:type="SOAP-ENC:Struct">
<ELEMENT1 xsi:type="xsd:int">697438</ELEMENT1>
<ELEMENT2 xsi:type="xsd:string">DDDD</ELEMENT2>
<ELEMENT3 xsi:type="xsd:string">CCCC</ELEMENT3>
<ELEMENT4 xsi:type="xsd:string">VVVVV</ELEMENT4>
<ELEMENT5 xsi:nil="true"/>
</return>
</ns1:informationXResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
使用Fiddler,我开始修改返回的XML,并在替换时成功地使其工作
<return xsi:type="SOAP-ENC:Struct">
通过
<return>