日期:2014-05-17 浏览次数:20901 次
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