Stock Sample Web Service
By Fons Sonnemans
Download
StockServices.zip
The Stock
Sample Web Service uses the Html2Xml service
to retrieve the Finance page of Yahoo. It then searches the Xml, using an XPath
expression, for the current value and returns it. Hopefully Yahoo never changes their HTML structure.
VB.NET Source Code
Imports System.Web.Services
Imports System.Xml
Imports Html2XmlService
Namespace ReflectionIT.WebServices.StockServices
<WebService(Namespace:="http://www.reflectionit.nl/webservices/",
Description:="StockServices created by Reflection IT.")>
_
Public Class StockServices
Inherits System.Web.Services.WebService
Public Sub New()
MyBase.New()
'This call is required by
the Web Services Designer.
InitializeComponent()
'Add your own initialization code after the
InitializeComponent() call
End
Sub
'Required by the Web Services Designer
Private
components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Web Services Designer
'It can be modified
using the Web Services Designer.
'Do not modify
it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>_
Private
Sub InitializeComponent()
components = New
System.ComponentModel.Container()
End Sub
Protected Overloads
Overrides Sub Dispose(ByVal
disposing As Boolean)
'CODEGEN: This procedure
is required by the Web Services Designer
'Do not modify
it using the code editor.
End
Sub
<WebMethod(Description:="Get the Yahoo stock quote.")> _
Public
Function GetStockQuote(ByVal symbol
As String) As Double
' Build the Url
Dim url As String
= "http://finance.yahoo.com/q?s=" +
symbol + "&d=v1"
' Get the XmlDocument using the Html2Xml WebServices
Dim
xml As XmlDocument = New
XmlDocument()
xml.LoadXml(New
Html2XmlService.Html2XmlServices().Url2XmlString(url))
' Find the Nodes using a XPath expression
Dim l As XmlNodeList = xml.SelectNodes("/html/body/center/table/tr/td/table/tr/td/b")
' Validate and return value
If (l.Count <> 2) Then
Throw New Exception("symbol
not found.")
End If
Return Double.Parse(l(1).InnerXml)
End If
End Function
End Class
End Namespace
Any suggestions and feedback for improving this article is most welcome. Send your
suggestions and feedback to Fons.Sonnemans@reflectionit.nl