Search code examples
c#asmx

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?


Solution

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