Home > ASP.Net > Use Yahoo Weather Forecast with ASP.Net

Use Yahoo Weather Forecast with ASP.Net


Hi all,

try this example to Use Yahoo Weather Forecast with ASP.Net

1) Open VS2008 and create new website

2) Add new Class and name it “WeatherItem.cs”


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public class WeatherItem
{
 #region Prob
 public string DayName { get; set; }
 public System.DateTime Day { get; set; }
 public int Low { get; set; }
 public int High { get; set; }
 public string Forcast { get; set; }
 public string Code { get; set; }
 public string ImageUrl { get; set; }
 public string LocalImageUrl { get; set; }
 protected string LocalImagePath = "Images/Weather/";
 #endregion

public WeatherItem()
 {
 //
 // TODO: Add constructor logic here
 //
 }

public WeatherItem(string localImagePath)
 {
 LocalImagePath = localImagePath;
 }
 public void YahooForcast(XElement forcastItem)
 {
 DayName = forcastItem.Attribute("day").Value;
 Day = Convert.ToDateTime(forcastItem.Attribute("date").Value);
 Low = Convert.ToInt32(forcastItem.Attribute("low").Value);
 High = Convert.ToInt32(forcastItem.Attribute("high").Value);
 Forcast = forcastItem.Attribute("text").Value;
 Code = forcastItem.Attribute("code").Value;
 ImageUrl = "http://l.yimg.com/a/i/us/we/52/" + Code + ".gif";
 LocalImageUrl = LocalImagePath + Code + ".gif";
 }
}

3) Add new User Control and name it “YahooWeather.ascx” and from Toolbox drag DataList and Label Control


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="YahooWeather.ascx.cs" Inherits="YahooWeather" %>
<style type="text/css">
 .wthrTitle { font-family: Tahoma; font-size: 14px; width: 370px; color: White; background: blue; border-top: 1px solid navy; text-align: center; }
 .wthrContainer { text-align: center; border: 1px; float: left; }
 .wthrDayTitle { font-family: Tahoma; font-size: 14px; width: 100%; color: White; background: blue; border-bottom: 1px solid navy; border-top: 1px solid navy; }
 .wthrForcast { font-family: Tahoma; font-size: 12px; color: blue; }
 .wthrTemp { font-family: Tahoma; font-size: 10px; color: blue; }
 .wthrClear { clear: left; }
 </style>
<div class="wthrTitle"><asp:Label ID="descriptionLabel" runat="server" Text='' /></div>
<asp:DataList ID="DL_Weather" runat="server" RepeatDirection="Horizontal"
 CellPadding="2" CellSpacing="2">
 <ItemTemplate>
 <div class="wthrContainer">
 <div class="wthrDayTitle"><asp:Label ID="dayLabel" runat="server" Text='<%# Eval("DayName") %>' /></div><br />
 <asp:Image ID="forcastImage" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' /><br />
 <asp:Label ID="forcastLabel" runat="server" Text='<%# Eval("Forcast") %>' CssClass="wthrForcast" /><br />
 <div class="wthrForcast"><asp:Label ID="highLabel" runat="server" Text='<%# Eval("High") %>' />° -
 <asp:Label ID="lowLabel" runat="server" Text='<%# Eval("Low") %>' />°</div><br />
 </div>
 </ItemTemplate>
</asp:DataList>
<br class="wthrClear" />
<asp:HiddenField ID="zipcodeHidden" runat="server" />

4) In code behind add the following code :-


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Collections.Generic;

public partial class YahooWeather : System.Web.UI.UserControl
{

#region "Properties"
 public string ZipCode
 {
 get { return zipcodeHidden.Value; }
 set { zipcodeHidden.Value = value; }
 }
 #endregion

 protected void Page_Load(object sender, EventArgs e)
 {
 GetWeather(ZipCode);
 }

 protected void GetWeather(string zipCode)
 {
 try
 {
 XDocument xmlData = XDocument.Load("http://xml.weather.yahoo.com/forecastrss/" + zipCode + "_f.xml");
 List<WeatherItem> _list = new List<WeatherItem>();
 string _description = null;
 System.DateTime _date;
 foreach (XElement _row in xmlData.Root.Elements("channel"))
 {
 foreach (XElement _field in _row.Elements())
 {
 if ((_field.Name.ToString() == "description"))
 {
 // cherry pick the desired data at this level.
 _description = _field.Value;
 descriptionLabel.Text = _description;
 }
 if ((_field.Name.ToString() == "lastBuildDate"))
 {
 string _dateString = _field.Value.Substring(5);
 _date = DateTime.Parse(_dateString.Substring(0, _dateString.Length - 4));
 descriptionLabel.Text = _description + " at " + _date.ToShortTimeString();
 }
 if (_field.Name.ToString() == "item")
 {
 foreach (XElement _itm in _field.Elements())
 {
 if (_itm.Name.LocalName == "forecast")
 {
 WeatherItem _wi = new WeatherItem();
 _wi.YahooForcast(_itm);
 _list.Add(_wi);
 }
 }
 }
 }
 }
 if (_list.Count > 0)
 {
 _list[0].DayName = "Today";
 }
 DL_Weather.DataSource = _list;
 DL_Weather.DataBind();
 }
 catch (Exception e)
 {

 }
 }
}

5) Add new Web Page and drag the YahooWeather.ascx UserControl inside it and browse your page

<uc1:YahooWeather ID="YahooWeather1" runat="server" ZipCode="39827" />

Hope this helps

Good Luck

 

Advertisements
Categories: ASP.Net
  1. Amir elshater
    April 30, 2012 at 8:10 am

    i got empty page

    • yasserzaid
      May 23, 2012 at 9:34 am

      @Amir : can you tell me if there is any error …. i hope to try it again and tell me

  2. sudha
    May 14, 2012 at 5:48 am

    Its very nice

    • yasserzaid
      May 23, 2012 at 9:35 am

      @sudha : Thank you hope it helps you

  3. yogesh
    May 31, 2012 at 6:01 am

    In the above given solution, Can we search by city name????

  4. loyd
    June 18, 2012 at 8:34 am

    after i added the code, there error im geting is :
    Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension ‘.ascx’ may be incorrect. Please review the URL below and make sure that it is spelled correctly.

  5. awais
    June 3, 2013 at 3:00 pm

    worked for me.
    @yasserzaid …. thanks a lot

  6. Aman
    April 1, 2014 at 5:42 am

    will you please elaborate some parts of code as i am getting plain page nothing displayed on it after execution

  7. October 31, 2016 at 12:25 pm

    I am regular visitor, how are you everybody?
    This post posted at this website is in fact nice.

  1. July 4, 2014 at 10:57 pm

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: