tags:

views:

776

answers:

2

Suppose I have an asmx web service at the following address: http://localhost/BudgetWeb/Service.asmx

This web service has a web method with the following signature:

string GetValue(string key)

This GetValue method returns a string like this:

<?xml version=\"1.0\" encoding=\"utf-8\" ?><value>250.00</value>

What if I wanted to do this:

XDocument doc = XDocument.Load("http://localhost/BudgetWeb/Service.asmx?op=GetValue&amp;key=key1")

This doesn't work, and I'm pretty sure that XDocument.Load doesn't actually invoke a web method on the server. I think it expects the uri to point to a file that it can load. To call a web method, I think I'd have to have a web proxy class and would have to use that to call string GetValue(string key), and then I could use that value returned from the web proxy class to pass to the XDocument.Load method.

Is my understanding correct, or is there a way for XDocument.Load to actually invoke a web method on the server?

+4  A: 

Try to use this:

XDocument doc = XDocument.Load(
        "http://localhost/BudgetWeb/Service.asmx/GetValue?key=key1");

EDIT: Just figured out: you're using a invalid URI:

http://localhost/BudgetWeb/Service.asmx?op=GetValue&amp;key=key1

Should be

http://localhost/BudgetWeb/Service.asmx/GetValue?key=key1

I'm using this code snippet:

string uri = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=MSFT";
XDocument doc1 = XDocument.Load(uri);
Console.WriteLine(doc1.Root.Value);  // <StockQuotes><Stock><Symbol>MSFT...
Rubens Farias
When I try that, I get System.Xml.XmlException: The 'input' start tag on line 71 does not match the end tag of 'td'. Line 71, position 98. It's basically the same error as I was getting without the XmlReader.Create part.
dcp
Your xml is not formatted properly, this is not a problem with the Load. Use http://validator.w3.org/ to validate a sample call.
Yuriy Faktorovich
Sorry for that, it was a typo. I still get same exception though.
dcp
Also, I tried it with the other URI you posted above but still get same error.
dcp
please, try this new code
Rubens Farias
Thanks for that sample, it put me on the right track. However, the final solution turned out to be something I needed to add to the web.config for the web service (see my answer).
dcp
+2  A: 

Ok, I found the issue. In the web.config for the web service, you have to add this:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>

Thanks to everyone for their suggestions, I really appreciate it, especially Rubens Farias whose working example put me on the right track.

dcp
You wrote it before I had a chance. But now you can see the Load works. Also why are you doing this instead of creating a Service Reference?
Yuriy Faktorovich
Yes, it works now and thanks again for the great help. The clientrequirement is that the web service call be invoked as a uri instead of using a service reference, so I don't have the option of using a service reference. I am mocking the web service for my testing, and I left out the protocol stuff which is why it wasn't working, in addition to having some problems with the uri itself as you correctly pointed out.
dcp
@dcp: If Rubens Farias example helped then you should mark that as an answer.
KMan
Agreed, I have marked it. Thanks again Rubens Farias.
dcp