首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么用c#编辑获取Webservice下的天气信息生成XML文档

2012-11-06 
如何用c#编辑获取Webservice上的天气信息生成XML文档题目如上,我是个新手,代码实现上有些困难,总是会出现

如何用c#编辑获取Webservice上的天气信息生成XML文档
题目如上,我是个新手,代码实现上有些困难,总是会出现错误,希望
1,用代码实现获取天气信息,能告诉下思路(希望不是自己创建Webservice和网上的同步)
2,给出部分代码参考,代码格式多点更好!
3,如何在VS2008上生成XML格式,告诉下具体操作.
4,能够加Timer控件,3小时刷新下数据(方便的话就给)


在线等答案,谢谢帮助!

[解决办法]
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
WeatherWebService wws = new WeatherWebService();
string[] weatherArray = wws.getWeatherbyCityName("");
Console.WriteLine("{0}-{1}-{2}", weatherArray[6], weatherArray[7], weatherArray[8]);
Console.Read();
htpwebrequest抓取天气数据
System.Threading.Timer tUpdatefile = new System.Threading.Timer(new TimerCallback(test), null, 0, 3*60*60 * 1000);
 private void test(object source)



[解决办法]
我来补充一下,怎么使用这个WebService
1. 添加Web引用:把这个WSDL拷贝到地址栏中:
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"

2. 然后重新命名一下这个WebService名,默认是Service1(这个是生成代码的namespace)
应该会生成一个app.config文件,

<bindings>配置节里有个customerBindings的配置节可以删去。

<endpoint>也有重复的,把那个WeatherWebServiceSoap12的节点删去。

3. 看下面的代码就可以用了。

C# code
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.Text.RegularExpressions;namespace CallWebServices{    public partial class Form1 : Form    {        private WeatherWebServices.WeatherWebServiceSoapClient weatherWebSvc = null;        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            try            {                weatherWebSvc = new CallWebServices.WeatherWebServices.WeatherWebServiceSoapClient();                weatherWebSvc.Open();                string[] cities = weatherWebSvc.getSupportCity("");                DataTable weatherData = new DataTable();                weatherData.Columns.Add("province");                weatherData.Columns.Add("city");                weatherData.Columns.Add("city_code");                weatherData.Columns.Add("city_image");                weatherData.Columns.Add("last_update_time");                weatherData.Columns.Add("temperature");                weatherData.Columns.Add("summary");                weatherData.Columns.Add("wind_direction");                weatherData.Columns.Add("wind_power");                weatherData.TableName = "WeatherData";                Regex regex = new Regex(@"(\d+)");                foreach (string city in cities)                {                    string cityCode = regex.Match(city).Value;                    string[] weatherDataArr = weatherWebSvc.getWeatherbyCityName(cityCode);                    DataRow row = weatherData.NewRow();                    for (int i = 0; i < weatherData.Columns.Count; i++)                        row[i] = weatherDataArr[i];                    weatherData.Rows.Add(row);                     //城市太多了,全查受不了,所以随便查几个就Stop了。                    if (weatherData.Rows.Count > 5)                        break;                }                //绑定查询结果,显示                this.dataGridView1.DataSource = weatherData;                //利用DataTable保存成Xml                weatherData.WriteXml(@"D:\temp\weather.xml");                weatherWebSvc.Close();            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }            finally            {                try                {                    weatherWebSvc.Close();                }                catch                { }            }                    }    }} 

热点排行