Click here to Skip to main content
Click here to Skip to main content
Go to top

Weather Info by using WeatherBug Service

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

Introduction

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("http://api.wxbug.net/getLiveWeatherRSS.aspx?ACode=" + 
                "{0}&zipCode={1}&UnitType=1&OutputType=1", ACode, codeValue);
else
    query = String.Format("http://api.wxbug.net/getLiveWeatherRSS" + 
       ".aspx?ACode={0}&cityCode={1}&UnitType=1&OutputType=1", ACode, codeValue);
            
XmlDocument weatherInfoXmlDoc = new XmlDocument();
XmlNamespaceManager man = new XmlNamespaceManager(weatherInfoXmlDoc .NameTable);
man.AddNamespace("aws", "http://www.aws.com/aws");
 
//download the XML from the web service
weatherInfoXmlDoc.Load(query);

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());
}
 
[HttpPost]
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, 
                                            model.CityOrZipCode.ToString());
    }
    else
    {
       weatherInfo = weatherBug.checkWeather(WeatherBug.WeatherTypeEnum.City,
                                            model.CityOrZipCode.ToString());   
    }
 
    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.

History

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

License

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

Share

About the Author

Ranjan.D
Web Developer
United States United States
Profile
 
Around 9 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
 
Others:
 
TTD, BDD
 
Education
 
B.E (Computer Science)
 
CodeProject Contest So Far:
 
1. Windows Azure Developer Contest - HealthReunion - A Windows Azure based healthcare product , link - http://www.codeproject.com/Articles/582535/HealthReunion-A-Windows-Azure-based-healthcare-pro
 
2. DnB Developer Contest - DNB Business Lookup and Analytics , link - http://www.codeproject.com/Articles/618344/DNB-Business-Lookup-and-Analytics
 
3. Intel Ultrabook Contest - Journey from development, code signing to publishing my App to Intel AppUp , link - http://www.codeproject.com/Articles/517482/Journey-from-development-code-signing-to-publishin
 
4. Intel App Innovation Contest 2013 - eHealthCare - http://www.codeproject.com/Articles/635815/eHealthCare
 
5. Grand Prize Winner of CodeProject HTML5 &CSS3 Article Content 2014

Comments and Discussions

 
Questionnot working PinmemberMember 103909369-Nov-13 10:12 
AnswerRe: not working PinprofessionalRanjan.D9-Nov-13 10:19 
GeneralWeather Service URL is not working PinmemberMember 103909369-Nov-13 10:58 
GeneralRe: Weather Service URL is not working PinprofessionalRanjan.D9-Nov-13 12:24 
It's not a web service and you cannot add a reference. Did you noticed that URL is a simple *.aspx page. All you need to do in make a web request with the parameters as shown in the tip.
 
Sorry my mistake in mentioning it as web service. I will be updating the tip.
 
Thanks,
Ranjan.D

GeneralRe: Weather Service URL is not working PinmemberMember 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 | Mobile
Web04 | 2.8.140905.1 | Last Updated 11 Nov 2013
Article Copyright 2013 by Ranjan.D
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid