Click here to Skip to main content
11,577,522 members (23,172 online)
Click here to Skip to main content

Weather Info by using WeatherBug Service

, 11 Nov 2013 CPOL 14.1K 12
Rate this:
Please Sign up or sign in to vote.
Real time weather information by consuming the Weather Bug service.


We will see how to fetch live weather information by city or zip code. In order to get weather information, we will be using making a web request to WeatherBug getLiveWeatherRSS.aspx supplying the required parameters. 

We need to pass in a few parameters like authentication code, city/zip code while making the request.

Using the code

Below is the code snippet we can use for building the request query and loading the response into an XmlDocument. The query string is formatted with Acode which is nothing but an authentication code, and codeValue can be either city/zip code.

String query = "";
if (typeValue == WeatherTypeEnum.ZipCode)
    query = String.Format("" + 
                "{0}&zipCode={1}&UnitType=1&OutputType=1", ACode, codeValue);
    query = String.Format("" + 
       ".aspx?ACode={0}&cityCode={1}&UnitType=1&OutputType=1", ACode, codeValue);
XmlDocument weatherInfoXmlDoc = new XmlDocument();
XmlNamespaceManager man = new XmlNamespaceManager(weatherInfoXmlDoc .NameTable);
man.AddNamespace("aws", "");
//download the XML from the web service

Here's the code snippet for building the WeatherInfo object. You can see below, we are selecting a node by XPath and then retrieving its InnerText.

XmlNode weather = weatherInfoXmlDoc.SelectSingleNode(weatherNode, man);
XmlNode ob = weather.SelectSingleNode(obNode, man);
XmlNode currentcondition = ob.SelectSingleNode(currentconditionNode, man);
currentconditionValue = currentcondition.InnerText;
XmlNode feelslike = ob.SelectSingleNode(feelslikeNode, man);
feelslikeValue = feelslike.InnerText;
XmlNode templow = ob.SelectSingleNode(templowNode, man);
templowValue = templow.InnerText;
XmlNode temphigh = ob.SelectSingleNode(temphighNode, man);
temphighValue = temphigh.InnerText;
XmlNode humidity = ob.SelectSingleNode(humidityNode, man);
humidityValue = humidity.InnerText;
XmlNode windspeed = ob.SelectSingleNode(windspeedNode, man);
windspeedValue = windspeed.InnerText;
XmlNode pressure = ob.SelectSingleNode(pressureNode, man);
pressureValue = pressure.InnerText;
XmlNode station = ob.SelectSingleNode(stationNode, man);
stationValue = station.InnerText;
XmlNode temp = ob.SelectSingleNode(tempNode, man);
tempValue = temp.InnerText;
//Sunrise Starts
XmlNode sunrise = ob.SelectSingleNode(sunriseNode, man);
XmlNode sunriseHour = sunrise.SelectSingleNode(sunriseHourNode, man);
sunriseHourValue = sunriseHour.Attributes["hour-24"].Value;
XmlNode sunriseMinutes = sunrise.SelectSingleNode(sunriseMinutesNode, man);
sunriseMinutesValue = sunriseMinutes.Attributes["number"].Value;
XmlNode sunriseSeconds = sunrise.SelectSingleNode(sunriseSecondsNode, man);
sunriseSecondsValue = sunriseSeconds.Attributes["number"].Value;
sunriseValue = sunriseHourValue + ":" + sunriseMinutesValue;
//Sunset Starts
XmlNode sunset = ob.SelectSingleNode(sunsetNode, man);
XmlNode sunsetHour = sunset.SelectSingleNode(sunsetHourNode, man);
sunsetHourValue = sunsetHour.Attributes["hour-24"].Value;
XmlNode sunsetMinutes = sunset.SelectSingleNode(sunsetMinutesNode, man);
sunsetMinutesValue = sunsetMinutes.Attributes["number"].Value;
XmlNode sunsetSeconds = sunset.SelectSingleNode(sunsetSecondsNode, man);
sunsetSecondsValue = sunsetSeconds.Attributes["number"].Value;
sunsetValue = sunsetHourValue + ":" + sunsetMinutesValue; 
WeatherInfo weatherInfo = new WeatherInfo
  type = ((WeatherTypeEnum)typeValue).ToString(),
  code = codeValue,
  currentCondition = currentconditionValue,
  feelsLike = feelslikeValue,
  tempHigh = temphighValue,
  tempLow = templowValue,
  humidity = humidityValue,
  pressure = pressureValue,
  windspeed = windspeedValue,
  temp = tempValue,
  station = stationValue,
  sunrise = sunriseValue,
  sunset = sunsetValue

Below is the MVC GET and POST action implementation. We are creating an instance of WeatherBug and making a call to checkWeather with two parameters.

public ActionResult Index(string returnUrl)
    ViewBag.ReturnUrl = returnUrl;
    return View(new WeatherInfoViewModel());
public ActionResult Index(WeatherInfoViewModel model)
    var weatherInfo = new WeatherBug.WeatherInfo();
    if (ModelState.IsValid == false)
        return View( new WeatherInfoViewModel
            CityOrZipCode= model.CityOrZipCode,
            WeatherInfoMethod = model.WeatherInfoMethod,  
            WeatherInfo = null
    var weatherBug = new WeatherBug.WeatherBug();
    if(model.WeatherInfoMethod == WeatherInfoMethod.ZipCode)
      weatherInfo = weatherBug.checkWeather(WeatherBug.WeatherTypeEnum.ZipCode, 
       weatherInfo = weatherBug.checkWeather(WeatherBug.WeatherTypeEnum.City,
    var viewModel = new WeatherInfoViewModel
         WeatherCode = model.CityOrZipCode,
         WeatherInfoMethod = model.WeatherInfoMethod,
         WeatherInfo = weatherInfo
    return View(viewModel);

Points of Interest

When I started to explore how to get weather information, the WeatherBug service was one thing which I could easily make use of and I loved coding the weather info app.


  • Version 1.0 - 08/23/2014 - Created an MVC4 sample weather info project.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States

Around 10 years of professional software development experience in analysis, design, development, testing and implementation of enterprise web applications for healthcare domain with good exposure to object-oriented design, software architectures, design patterns, test-driven development and agile practices.

In Brief

Analyse and create High Level , Detailed Design documents.
Use UML Modelling and create Use Cases , Class Diagram , Component Model , Deployment Diagram, Sequence Diagram in HLD.

Area of Working : Dedicated to Microsoft .NET Technologies
Experience with : C# , J2EE , J2ME, Windows Phone 8, Windows Store App
Proficient in: C# , XML , XHTML, XML, HTML5, Javascript, Jquery, CSS, SQL, LINQ, EF

Software Development

Database: Microsoft SQL Server, FoxPro
Development Frameworks: Microsoft .NET 1.1, 2.0, 3.5, 4.5
UI: Windows Forms, Windows Presentation Foundation, ASP.NET Web Forms and ASP.NET MVC3, MVC4
Coding: WinForm , Web Development, Windows Phone, WinRT Programming, WCF, WebAPI

Healthcare Domain Experience

CCD, CCR, QRDA, HIE, HL7 V3, Healthcare Interoperability


B.E (Computer Science)

CodeProject Contest So Far:

1. Windows Azure Developer Contest - HealthReunion - A Windows Azure based healthcare product , link -

2. DnB Developer Contest - DNB Business Lookup and Analytics , link -

3. Intel Ultrabook Contest - Journey from development, code signing to publishing my App to Intel AppUp , link -

4. Intel App Innovation Contest 2013 - eHealthCare

5. Grand Prize Winner of CodeProject HTML5 &CSS3 Article Contest 2014

6. Grand Prize Winner of CodeProject Android Article Contest 2014

7. Grand Prize Winner of IOT on Azure Contest 2015

You may also be interested in...

Comments and Discussions

Questionnot working Pin
Member 103909369-Nov-13 10:12
memberMember 103909369-Nov-13 10:12 
AnswerRe: not working Pin
Ranjan.D9-Nov-13 10:19
professionalRanjan.D9-Nov-13 10:19 
Sorry I noticed there was a small typo. It should be *.aspx. The query should be constructed as

String.Format("" +
                "{0}&zipCode={1}&UnitType=1&OutputType=1", ACode, codeValue);

Please download the source code you that you can have a feel.


GeneralWeather Service URL is not working Pin
Member 103909369-Nov-13 10:58
memberMember 103909369-Nov-13 10:58 
GeneralRe: Weather Service URL is not working Pin
Ranjan.D9-Nov-13 12:24
professionalRanjan.D9-Nov-13 12:24 
GeneralRe: Weather Service URL is not working Pin
Member 103909369-Nov-13 13:01
memberMember 103909369-Nov-13 13:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 11 Nov 2013
Article Copyright 2013 by Ranjan.D
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid