Web Services Call including the XML and Linq namespaces

I like using ASMX Web Services (WS)in my code -- either creating them or just calling them.
(Yes, I use Windows Communication Foundation (WCF) also).

One of the best features of the WS call is that the object model can be exposed to allow the caller to return specific results that need no additional parsing.  This does not hold up, however, if the result is returned in a string of raw XML as the result may require additional parsing to be of best use.

Here is an example of calling Web Services where:

  1. The Web Service returns raw XML without object formatting
    (called by HTTP)
    and the XmlDocument is used to parse the result.
  2. The Web Service returns raw XML without object formatting
    (called by SOAP)
    and the XmlDocument is used to parse the result.
  3. The Web Service returns a regular object
    and no active XML treatment is needed.

I created this using using Visual Studio 2008 Standard calling to two different Web Services that provide Stock Quotes.
Here is an AltaVista query that can be used to find Web Services on the web.


WS_SQ Properties


using System;
using System.Linq;
using System.Xml.Linq;
//
//using Snag.WS_SQ;
using Snag.WS_Nexus6;

namespace Snag
{
   class CSnag
   {
      /// <summary>
      /// Main()
      /// </summary>
      static void Main(string[] args)
      {
         string strStockSymbol = "ERIC";//Ericsson
         Console.WriteLine("WS=" + GetWsToXmlLastValue(strStockSymbol));
         Console.WriteLine("XD=" + GetXdToXmlLastValue(strStockSymbol));
         Console.WriteLine("SO=" + GetWsToSoapLastValue(strStockSymbol));
      }


      /// <summary>
      /// Makes a call to a web service that implements the SOAP HTTP binding
      /// and parses the XML (string) response.
      ///
      /// This example is for calls that return XML not returned in
      /// a pre-parsed object.
      /// </summary>
      /// <param name="strStockSymbol"></param>
      /// <returns>the last value of the stock</returns>
      private static string GetXdToXmlLastValue(string strStockSymbol)
      {
         XDocument xd = XDocument.Parse( XDocument.Load(
            "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol="
            + strStockSymbol).Descendants().First().Value);
        
         return xd
            .Element("StockQuotes").Element("Stock").Element("Last").Value;
      }


      /// <summary>
      /// Makes a regular WebService call using SOAP
      /// and parses the XML (string) response.
      ///
      /// This example is for calls that return XML not returned in
      /// an object
      /// </summary>
      /// <param name="strStockSymbol"></param>
      /// <returns>the last value of the stock</returns>
      private static string GetWsToXmlLastValue(string strStockSymbol)
      {
         using (WS_SQ.StockQuote ws = new WS_SQ.StockQuote())
         {
            XDocument xd =
               XDocument.Parse(ws.GetQuote(strStockSymbol));

            return xd
               .Element("StockQuotes").Element("Stock").Element("Last").Value;
         }
      }


      /// <summary>
      /// Makes a regular WebService call using SOAP
      /// where an object is returned.
      /// </summary>
      /// <param name="strStockSymbol"></param>
      /// <returns>the last value of the stock</returns>
      private static string GetWsToSoapLastValue(string strStockSymbol)
      {
         using (StockQuoteService sqs = new StockQuoteService())
         {
            return sqs.GetStockQuote(strStockSymbol).LastTrade;
         }
      }
   }
}

posted @ Friday, January 1, 2010 3:47 PM
Print

Comments on this entry:

No comments posted yet.

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview: