Stock Sample Web Service

By Fons Sonnemans, posted on
1592 Views

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 <_o3a_p> </_o3a_p>

Namespace ReflectionIT.WebServices.StockServices <_o3a_p />

<WebService(Namespace:="https://www.reflectionit.nl/webservices/", Description:="StockServices created by Reflection IT.")> _
Public Class StockServices <_o3a_p> </_o3a_p>

Inherits System.Web.Services.WebService <_o3a_p> </_o3a_p>

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 <_o3a_p />

 

'Required by the Web Services Designer
Private components As System.ComponentModel.Container <_o3a_p> </_o3a_p>

'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. <_o3a_p />

<System.Diagnostics.DebuggerStepThrough()>_
Private Sub InitializeComponent()
   components = New System.ComponentModel.Container()
End Sub <_o3a_p />

 

Protected Overloads Overrides Sub Dispose(ByVal disposing AsBoolean)
   'CODEGEN: This procedure is required by the Web Services Designer
  
'Do not modify it using the code editor.
End Sub <_o3a_p />

 

<WebMethod(Description:="Get the Yahoo stock quote.")> _
Public Function GetStockQuote(ByVal symbol AsString) AsDouble <_o3a_p />

 

   ' Build the Url
   Dim url AsString = "http://finance.yahoo.com/q?s=" + symbol + "&d=v1" <_o3a_p />

 

   ' Get the XmlDocument using the Html2Xml WebServices
  
Dim xml As XmlDocument = New XmlDocument()
  
xml.LoadXml(New Html2XmlService.Html2XmlServices().Url2XmlString(url)) <_o3a_p />

 

   ' Find the Nodes using a XPath expression
   Dim l As XmlNodeList = xml.SelectNodes("/html/body/center/table/tr/td/table/tr/td/b") <_o3a_p />

 

   ' Validate and return value
   If (l.Count <> 2) Then
      Throw New Exception("symbol not found.")
   End If
   Return Double.Parse(l(1).InnerXml) <_o3a_p />

End If <_o3a_p />

End Function <_o3a_p />

End Class <_o3a_p />

 

End Namespace

Any suggestions and feedback for improving this article is most welcome. Send your suggestions and feedback to Fons.Sonnemans@reflectionit.nl

Download

Tags

Web XML ASP.NET

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.

Leave a comment

Blog comments

0 responses