c# 实现天气预报
先添加Web引用http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
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; namespace WindowsFormsApplication1 { public partial class FrmWeahter : Form { public FrmWeahter() { InitializeComponent(); } //实例化天气预报服务 WeatherWebService.WeatherWebService weatherWebService = new WeatherWebService.WeatherWebService(); /// /// 获取省 /// private void FrmWeahter_Load(object sender, EventArgs e) { string[] proveces = weatherWebService.getSupportProvince();// 获取省 foreach (string provece in proveces) { lbProvece.Items.Add(provece); } } /// /// 获取市 /// private void lbProvece_SelectedIndexChanged(object sender, EventArgs e) { lbCity.Items.Clear(); string[] citys = weatherWebService.getSupportCity(lbProvece.SelectedItem.ToString());//获取市 foreach (string city in citys) { lbCity.Items.Add(city.Substring(0, city.IndexOf(' '))); } } /// /// 查询天气结果 /// private void lbCity_SelectedIndexChanged(object sender, EventArgs e) { txtWeather.Clear(); string[] weathers = weatherWebService.getWeatherbyCityName(lbCity.SelectedItem.ToString());// 获取天气 if (weathers[1].Trim() == "") { MessageBox.Show("操作太快"); return; } //获取所有返回信息 for (int i = 0; i < weathers.Length; i++) { txtWeather.Text += weathers[i] + " " + i.ToString() + "\r\n\r\n"; } string path = Application.StartupPath + @"\weather\"; pictureBox1.Image = new Bitmap(path + weathers[8]);//天气趋势开始图片 pictureBox2.Image = new Bitmap(path + weathers[9]);//天气趋势结束图片 lblTemp.Text = weathers[5];//气温 lblCity.Text = lbCity.SelectedItem.ToString();//城市 } } }
气图标可至【http://www.webxml.com.cn/images/weather.zip】下载。