用C#实现天气预报(调用WebService)

添加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.Text;
using System.Windows.Forms;

namespace 天气助手
{
    public partial class Weather : Form
    {
        public Weather()
        {
            InitializeComponent();
        }

        public string[] GetWeather(string xCity)
        {
            cn.com.webxml.www.WeatherWebService mWeatherWebService = new cn.com.webxml.www.WeatherWebService();
            string[] WeatherOfCity = mWeatherWebService.getWeatherbyCityName(xCity);
            return WeatherOfCity;
        }

        private void Weather_Load(object sender, EventArgs e)
        {


        }

        public void Weather_Shown(object sender, EventArgs e)
        {
            try
            {
                cn.com.webxml.www.WeatherWebService mWeatherWebService = new cn.com.webxml.www.WeatherWebService();
                string[] mArea = mWeatherWebService.getSupportProvince();

                int mCount = mArea.Length - 1;

                comboBox01.Items.Clear();
                for (int mI = 0; mI <= mCount; mI++)
                {
                    comboBox01.Items.Add(mArea[mI].ToString());

                }
                comboBox01.SelectedIndex = 0;

                               
            }
            catch
            {

            }
        }

        public void comboBox01_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cn.com.webxml.www.WeatherWebService mWeatherWebService = new cn.com.webxml.www.WeatherWebService();
                string[] mCity = mWeatherWebService.getSupportCity(comboBox01.Text);

                int mCount = mCity.Length - 1;

                comboBox02.Items.Clear();
                for (int mI = 0; mI <= mCount; mI++)
                {
                    comboBox02.Items.Add(mCity[mI].Remove(mCity[mI].IndexOf("(")));

                }
                comboBox02.SelectedIndex = 0;
            }
            catch
            {

            }

        }

        public void comboBox02_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string[] WeatherOfCity = GetWeather(comboBox02.Items[comboBox02.SelectedIndex].ToString());

                label03.Text = WeatherOfCity[0].ToString();
                label05.Text = WeatherOfCity[1].ToString();

                label06.Text = WeatherOfCity[10].ToString();

                //今天
                pictureBox01.ImageLocation = @"images/weather/" + WeatherOfCity[8].ToString();
                pictureBox02.ImageLocation = @"images/weather/" + WeatherOfCity[9].ToString();
                label07.Text = WeatherOfCity[6].ToString() + WeatherOfCity[5].ToString() + WeatherOfCity[7].ToString();
                label08.Text = WeatherOfCity[11].ToString();             

                //明天
                pictureBox03.ImageLocation = @"images/weather/" + WeatherOfCity[15].ToString();
                pictureBox04.ImageLocation = @"images/weather/" + WeatherOfCity[16].ToString();
                label16.Text = WeatherOfCity[13].ToString() + WeatherOfCity[12].ToString() + WeatherOfCity[14].ToString();

                //后天
                pictureBox05.ImageLocation = @"images/weather/" + WeatherOfCity[20].ToString();
                pictureBox06.ImageLocation = @"images/weather/" + WeatherOfCity[21].ToString();
                label17.Text = WeatherOfCity[18].ToString() + WeatherOfCity[17].ToString() + WeatherOfCity[19].ToString();

                //城市说明及图片
                pictureBox07.ImageLocation = @"http://www.cma.gov.cn/tqyb/img/city/" + WeatherOfCity[3].ToString();                
                label18.Text = WeatherOfCity[22].ToString();

                //预计时间
                label19.Text = "预报时间:" + WeatherOfCity[4].ToString();

            }
            catch
            {

            }
        }

    }
}

-

转载于:https://www.cnblogs.com/ServerMaintaining/archive/2010/11/08/1871473.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# WebService 实现天气预报的一般步骤如下: 1. 获取天气数据源:可以从第三方天气API获取天气数据。 2. 创建 WebService 项目:在 Visual Studio 中创建一个 WebService 项目。 3. 定义 WebService:在 WebService 项目中定义一个 WebService,提供获取天气信息的方法。 4. 调用 API:在 WebService 的方法中调用天气 API 获取实时天气信息。 5. 返回结果:将获取到的天气信息返回给客户端。 以下是一个简单的 C# WebService 实现天气预报的例子: ```csharp using System; using System.Web.Services; using System.Xml; namespace WeatherWebService { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WeatherService : System.Web.Services.WebService { [WebMethod] public string GetWeather(string city) { string url = "http://wthrcdn.etouch.cn/weather_mini?city=" + city; XmlDocument xml = new XmlDocument(); xml.Load(url); XmlNodeList xnList = xml.SelectNodes("/resp/forecast"); string result = ""; foreach (XmlNode xn in xnList) { result += xn.SelectSingleNode("date").InnerText + "\n"; result += xn.SelectSingleNode("high").InnerText + "\n"; result += xn.SelectSingleNode("low").InnerText + "\n"; result += xn.SelectSingleNode("type").InnerText + "\n"; result += "\n"; } return result; } } } ``` 在上面的例子中,我们定义了一个名为“GetWeather”的 WebService 方法,该方法接受一个城市名称作为参数,并返回该城市未来几天的天气预报信息。我们使用 XmlDocument 类从天气 API 获取 XML 数据,然后解析 XML 并返回结果给客户端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值