Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am writing a .NET web application that needs to call external webservices. The documentation I have been provided with includes code examples in PHP.

I can successfully create a web reference in VS2010 using the WSDL address that has been provided to me, and using fiddler I can see that the expected XML is getting sent and received. However .NET appears to be having trouble parsing the returned XML.

The simplest web service I'm dealing with just accepts an array of usernames and is meant to return some nested hash arrays of users (with each user it's own array name, type, etc. fields) and an array of errors (for any usernames that didn't match up). The documentation I have describes it in 'PHP-ish':

array (
  'users' => array (
    array(
      'id' => 11,
      'username' => 'mick',
      'firstname' => 'Mick',
      'lastname' => 'Byrne'
    ),
    ...
  )
  'errors' => array(
    array(
      'username' => 'whoever',
      'errorcode' => 'NOSUCHUSER'
    )
  )
)

I'm getting the SOAP XML that would correspond to this. However, when .NET tries to turn it into a result it throws an exception:

Cannot assign object of type System.Xml.XmlNode[] to an object of type System.String.

Interestingly, the corresponding method that .NET has created for me based on the WSDL says it returns a plain old string which suggests that it can't handle the way the WSDL defines the return type.

The full WSDL is available here:

http://www.elearning.psychology.org.au/webservice/soap/server.php?wsdl=1&wstoken=dc45858adb6f28b7feae87014d46d9b3

Here is a sample of the sent and returned XML from the this basic Get Usernames request:

<?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>

And response:

<?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>

Any help would be greatly appreciated.

share|improve this question
Seeing the WSDL and/or SOAP message would be helpful. – JohnOpincar Mar 29 '11 at 2:11
Good point, they're now added in. – Mick Byrne Mar 29 '11 at 2:22
I've found some other StackOverflow issues that appears to be very similar to mine, though none have any kind of practical solution, such as this RPC related one and another specifically about WSDL arrays and another about PHP webservices consumed in .NET. – Mick Byrne Mar 29 '11 at 3:44
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

These may help:

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

share|improve this answer
Thanks @joelt, great links... In fact I went and ran the wsdl.exe tool and it clearly says at the top Warning: This web reference does not conform to WS-I Basic Profile v1.1.. I'm pretty sure this is the source of the problem, and as the Microsoft MVP says in your first link "The problem is that you've hit a known bug when consuming an RPC/Encoded web service. It's not going to work. There are no workarounds, other than to use web services which comply with WS-I BP 1.1." – Mick Byrne Mar 29 '11 at 2:32
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.