我正在编写一个需要调用外部 Web 服务的.NET Web 应用程序。我提供的文档包括 PHP 中的代码示例。
我可以使用提供给我的 WSDL 地址在 VS2010 中成功创建 Web 引用,并且使用 fiddler 我可以看到预期的 XML 正在发送和接收。但是.NET 似乎在解析返回的 XML 时遇到问题。
我正在处理的最简单的 Web 服务只接受一个用户名数组,并且意味着返回一些嵌套的用户哈希数组(每个用户拥有自己的数组名,类型等字段)和一组错误(对于任何用户名)那不匹配)。我在'PHP-ish'中描述的文档:
array (
'users' => array (
array(
'id' => 11,
'username' => 'mick',
'firstname' => 'Mick',
'lastname' => 'Byrne'
),
...
)
'errors' => array(
array(
'username' => 'whoever',
'errorcode' => 'NOSUCHUSER'
)
)
)
我正在获得与此对应的 SOAP XML。但是,当.NET 尝试将其转换为结果时,它会抛出异常:
Cannot assign object of type System.Xml.XmlNode[] to an object of type System.String.
有趣的是,.NET 基于 WSDL 为我创建的相应方法说它返回一个普通的旧string
,这表明它无法处理 WSDL 定义返回类型的方式。
完整的 WSDL 可在此处获得:
以下是此基本 Get Usernames 请求中已发送和返回的 XML 的示例:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3" xmlns:types="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:netspot_user_get_users_by_username>
<usernames href="#id1" />
</tns:netspot_user_get_users_by_username>
<soapenc:Array id="id1" soapenc:arrayType="xsd:string[1]">
<Item>557788</Item>
</soapenc:Array>
</soap:Body>
</soap:Envelope>
并回应:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.elearning.psychology.org.au/webservice/soap/server.php?wstoken=dc45858adb6f28b7feae87014d46d9b3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:netspot_user_get_users_by_usernameResponse>
<return xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">errors</key>
<value SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">username</key>
<value xsi:type="xsd:string">557788</value>
</item>
<item>
<key xsi:type="xsd:string">errorcode</key>
<value xsi:type="xsd:string">NOSUCHUSER</value>
</item>
</item>
</value>
</item>
</return>
</ns1:netspot_user_get_users_by_usernameResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
任何帮助将不胜感激。
2 回答
这可能有所帮助:
http://social.msdn.microsoft.com/Forums/en/asmxandxml/thread/f550e2b2-af9e-4653-a618-cffffdc70fdf
http://bytes.com/topic/net/answers/426522-system-invalidcastexception-cannot-assign-object-type-system-xml-xmlnode-object-type-system-string
有同样的问题。我所要做的就是在生成的 cs 文件中修复从
https
到http
的每个命名空间。所以,它可能是你的命名空间错了。