如何用c#编辑获取Webservice上的天气信息生成XML文档

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


在线等答案,谢谢帮助!
...全文
1598 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangjidi1985 2012-10-24
  • 打赏
  • 举报
回复
学习一下,详细
LIMINLY520 2011-07-26
  • 打赏
  • 举报
回复
虽结贴了
但还是关注一下啊 ……
a_110120 2011-02-16
  • 打赏
  • 举报
回复
你就像那一把火!
a_110120 2010-11-10
  • 打赏
  • 举报
回复
<book ID="45"><Author>4545</Author><Title>45464</Title></book>
zhshj444 2010-10-31
  • 打赏
  • 举报
回复
不错,学习了
xml20 2010-10-30
  • 打赏
  • 举报
回复
通过webservices取值,存到xml
yusheng622 2010-10-30
  • 打赏
  • 举报
回复
kankan
潇洒王子 2010-10-29
  • 打赏
  • 举报
回复
这个我以前也搞过,当时是按行生产的汉字,其他的都过滤掉了,08年的事了,现在那程序都不知道扔哪里了
candytangli 2010-10-29
  • 打赏
  • 举报
回复
hehe
不帮忙先生 2010-10-29
  • 打赏
  • 举报
回复
前排mark一下...
mlhy20060406 2010-10-28
  • 打赏
  • 举报
回复
留个记号,将来学习时会用到
kakajay008 2010-10-28
  • 打赏
  • 举报
回复
mark一下,好像还不错的样子
fhfdq163 2010-10-28
  • 打赏
  • 举报
回复
很详细。很好。
a_110120 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 a_110120 的回复:]
都去吃饭去了么。。。
[/Quote]
大神,是你啊~ 在我帖子里出现了 激动无比。
wuyq11 2010-10-15
  • 打赏
  • 举报
回复
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)


a_110120 2010-10-15
  • 打赏
  • 举报
回复
都去吃饭去了么。。。
a_110120 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fangxinggood 的回复:]
我来补充一下,怎么使用这个WebService
1. 添加Web引用:把这个WSDL拷贝到地址栏中:
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"

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

……
[/Quote]
先谢谢,有些地方还没看懂,先给分了,写的太好 我学的太浅薄了 有点看不懂。


System.Threading.Timer tUpdatefile = new System.Threading.Timer(new TimerCallback(test), null, 0, 3*60*60 * 1000); 能帮我拆分解释下么。
机器人 2010-10-15
  • 打赏
  • 举报
回复
我来补充一下,怎么使用这个WebService
1. 添加Web引用:把这个WSDL拷贝到地址栏中:
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"

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

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

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

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


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格式:


<?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>
a_110120 2010-10-15
  • 打赏
  • 举报
回复
如果说WeatherWebService 缺少程序集引用,该怎么办?
a_110120 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
WeatherWebService wws = new WeatherWebService();
string[] weatherArray = wws.getWeatherbyCityName("");
Console.WriteLine("{0}-{1}-{2}", w……
[/Quote]
有部分没看懂 能QQ和你联系么~我QQ是349954464~

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧