【发布时间】:2014-03-06 23:12:09
【问题描述】:
我终于从 VB.net 跳到了 C#,所以我仍然遇到一些问题。我正在制作一个通过 RSS 提要连接的简单天气应用程序。但是,我希望它返回一个标签,以确定它是否在外面冻结;我在将温度字符串转换为整数时遇到问题,因此我可以确定温度是否小于或等于 32 度。有什么想法吗?
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!";
}
}
}
}
【问题讨论】:
-
“问题”是什么?
-
抱歉,我的标题有错字。该值当前不是整数,而是字符串。所以我不能使用像 这样的比较对象
-
也看看this post。如果此处的答案解决了您的问题,请不要忘记将其标记为已接受(并最终投票)!
-
必须停止思考 vb 和 C# 开始思考 .Net 伙伴
标签: c# string int type-conversion