I have finally taken the jump from VB.net to C# so I'm still having some issues. I am making a simple weather app that connects through a RSS feed. I want it to return a label that determines if it is freezing outside, however; I am having issues converting the temperature string to an integer so I can determine if the temperature is less than or equal to 32 degrees. Any ideas?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace WeatherApp
{
public partial class frmWeather : Form
{
string Temperature;
public frmWeather()
{
InitializeComponent();
}
private void getWeather()
{
string query = string.Format("http://weather.yahooapis.com/forecastrss?w=" + txtZip.Text);
XmlDocument wData = new XmlDocument();
wData.Load(query);
XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);
Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
}
private void tmrWeather_Tick(object sender, EventArgs e)
{
getWeather();
DateTime now = DateTime.Now;
lblTemp.Text = "" + Temperature;
if (lblTemp.Text <= "32")
{
lblResult.Text = "It is freezing outside!";
}
}
}
}
You can use Convert.ToInt32() method to convert your String into integer
Try This:
//lblTemp.Text = "" + Temperature; this statement is not required.
if (Convert.ToInt32(Temperature) <= 32)
{
lblResult.Text = "It is freezing outside!";
}
OR
you can use int.TryParse() method to perform the proper conversion even if the data is invalid.
Try This:
int temp;
if (int.TryParse(Temperature,out temp))
{
if(temp <= 32)
lblResult.Text = "It is freezing outside!";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With