110,536
社区成员
发帖
与我相关
我的任务
分享
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
{ }
}
}
}
}
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<WeatherData>
<province>直辖市</province>
<city>北京</city>
<city_code>54511</city_code>
<city_image>54511.jpg</city_image>
<last_update_time>2010-10-15 14:04:50</last_update_time>
<temperature>10℃/22℃</temperature>
<summary>10月15日 晴转多云</summary>
<wind_direction>无持续风向微风</wind_direction>
<wind_power>0.gif</wind_power>
</WeatherData>
<WeatherData>
<province>直辖市</province>
<city>上海</city>
<city_code>58367</city_code>
<city_image>58367.jpg</city_image>
<last_update_time>2010-10-15 14:18:38</last_update_time>
<temperature>15℃/24℃</temperature>
<summary>10月15日 多云转晴</summary>
<wind_direction>东北风3-4级转东南风3-4级</wind_direction>
<wind_power>1.gif</wind_power>
</WeatherData>
<WeatherData>
<province>直辖市</province>
<city>天津</city>
<city_code>54517</city_code>
<city_image>54517.jpg</city_image>
<last_update_time>2010-10-15 14:04:08</last_update_time>
<temperature>11℃/22℃</temperature>
<summary>10月15日 晴转多云</summary>
<wind_direction>西南风3-4级转东北风3-4级</wind_direction>
<wind_power>0.gif</wind_power>
</WeatherData>
<WeatherData>
<province>直辖市</province>
<city>重庆</city>
<city_code>57516</city_code>
<city_image>57516.jpg</city_image>
<last_update_time>2010-10-15 14:01:43</last_update_time>
<temperature>17℃/23℃</temperature>
<summary>10月15日 阴转多云</summary>
<wind_direction>无持续风向微风</wind_direction>
<wind_power>2.gif</wind_power>
</WeatherData>
<WeatherData>
<province>特别行政区</province>
<city>香港</city>
<city_code>45005</city_code>
<city_image>45005.jpg</city_image>
<last_update_time>2010-10-15 14:00:14</last_update_time>
<temperature>24℃/27℃</temperature>
<summary>10月15日 阵雨转多云</summary>
<wind_direction>东北风4-5级</wind_direction>
<wind_power>3.gif</wind_power>
</WeatherData>
<WeatherData>
<province>特别行政区</province>
<city>澳门</city>
<city_code>45011</city_code>
<city_image>45011.jpg</city_image>
<last_update_time>2010-10-15 14:02:48</last_update_time>
<temperature>23℃/27℃</temperature>
<summary>10月15日 阵雨转多云</summary>
<wind_direction>东北风4-5级</wind_direction>
<wind_power>3.gif</wind_power>
</WeatherData>
</DocumentElement>