dcp
dcp

Reputation: 55424

How to call a web service using XDocument?

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

Upvotes: 3

Views: 4077

Answers (2)

dcp
dcp

Reputation: 55424

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.

Upvotes: 3

Rubens Farias
Rubens Farias

Reputation: 57936

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&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...

Upvotes: 5

Related Questions