Non-Web Referenced Webservices in VB.NET

Webservices are extremely common in modern day web-based development. Especially with the emergence of AJAX and mashup-style development. You can call webservices in a variety of ways, a few include:

  • Javascript and AJAX Script Manager (see A Recipe for AJAX Webservices)
  • Web References (by adding a project web reference through Visual Studio)
  • Ad-hoc (through server-side code, and will be the content for this article)

Ad-hoc Webservices

There are always instances where you want to use differing methods for producing the same results. In this case, we want to be able to access data from a webservice in a server-side environment and possibly perform some actions upon the data that exceed the realm of javascript or similar clientside scripting language.

Weather Webservice Example

For the purpose of demonstration, I’m going to use a random webservice I had in some old code that retrieves the weather conditions for a specific geographical area (city/country). (hopefully, at the time you read this the webservice is still active and free).

The url for the webservice ASMX is the following:

HTML Display for the Results

We need to display the result that the webservice returns, we’ll do this in a very simple way:

Weather Condition: <asp:Label ID="lblWeatherCondition" runat="server"></asp:Label>

Time to Wire Up the Webservice Code

This is one way to wire up this webservice. There are other ways as well, please feel free to leave comments on additional improvements to this method.

Note: The data returned by this webservice appears to wrap data returned from another webservice (probably not the best example to use, but this happens in the real world). So I’m actually doing a little extra digging by adding a second XMLDocument that references the XML we care about.

Private Sub getWeatherService()
    'Get Webservice URL (Add parameters based on city/country)
    Dim strURL As String = String.Format("{0}&CountryName={1}", "New York", "United States")

    'Create original XML document (this could also be an xmltextreader) - usually this is end of step, but this webservice is crap and needs two
    Dim XMLDoc As New XmlDocument()

    'Get true XML for webservice we care about
    Dim strXML As String = XMLDoc.ChildNodes(1).ChildNodes(0).InnerText

    'Load new XML document with appropriate XML content
    XMLDoc = New XmlDocument()

    'Pull out XML inner text (should be value, but spaces inappropriately exist) - uses XPATH
    Dim strWeatherCondition As String = XMLDoc.SelectSingleNode("/CurrentWeather/SkyConditions").InnerText

    'Assign value to label - duh.
    lblWeatherCondition.Text = strWeatherCondition
End Sub

You can now place this webservice call wherever you need to trigger the event.


Demonstrations and articles often stop well short of real-world application. Please feel free to take this example and build on it to do several other actions after receiving the data. For example, you could:

  • …use the value to display a corresponding image for the weather to graphical represent the data.
  • …use the value to pass into more detailed information to return historical data for similar conditioned days, or more detailed information such as barometric pressure, rainfall expectations, etc.
  • …use the value to redirect the user to different areas of the website or change graphical themes by replacing CSS files or switching Themes in .NET.
  • …use the value to do whatever your creative mind comes up with.

Please leave me comments on some of the additional applications you come up with; have working examples, leave the link so we can all check them out!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s