C#调用国家气象局天气预报接口

时间:2022-11-04 06:15:50

原文:C#调用国家气象局天气预报接口

一.需求

最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等。

例如:查询北京市天气

结果为:

今日北京天气:15℃~5℃ 阵雨转阴,北风3-4级,天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣 或厚羽绒服。 洗车指数:不宜。紫外线指数:最弱。晨练指数:较不宜.舒适指数:较舒适。

还好,国家气象局提供相关接口,再此表示感谢,虽然个人觉得该api设计思路有待提高,但是相比于其他部委【如交通部】来说,气象局这个冷衙门有开源、开放的心态,相比而言,相当了不起。期待后期能在接口规范、可提供查询城市数量等方面继续提高。

二.接口说明

据我所知,气象局提供三个天气查询接口,如下:

1、实时天气接口

地址:http://www.weather.com.cn/data/ sk/【城市代码】.html

例如:查询北京实时天气

http://www.weather.com.cn/data/sk/101010100.html

结果:

{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"13","WD":"南风","WS":"2","SD":"59%","WSE":"2","time":"10:30","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}

2、今日天气接口

地址:http://www.weather.com.cn/data/cityinfo/【城市代码】.html

例如:查询北京今日天气

http://www.weather.com.cn/data/cityinfo/101010100.html

结果:

{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"15","temp2":"5℃","weather":"阵雨转阴","img1":"d3.gif","img2":"n2.gif","ptime":"08:00"}}

3、今日及未来天气接口【内容最详细】

地址:http://m.weather.com.cn/data/【城市代码】.html

例如:查询北京今日及未来天气

http://m.weather.com.cn/data/101010100.html

结果:

{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2013年10月22日","date":"","week":"星期二","fchh":"08","cityid":"101010100","temp1":"15℃~5℃","temp2":"15℃~4℃………【省略】"index_tr":"适宜","index_co":"较舒适","st1":"11","st2":"1","st3":"13","st4":"5","st5":"14","st6":"4","index_cl":"较不宜","index_ls":"不太适宜","index_ag":"不易发"}}

字段说明:我刚好是调用这个接口,所以下面通过对应的实体及属性来说明上述返回结果中各字段的含义

C#调用国家气象局天气预报接口
/// <summary>
/// 天气查询结果信息实体
/// </summary>
public class WeatherinfoModel
{
/// <summary>
/// 城市名称
/// </summary>
public String city{get;set;} /// <summary>
/// 城市英文名称==拼音
/// </summary>
public String city_en{get;set;} /// <summary>
/// 今日时间【年-月-日】
/// </summary>
public String date_y{get;set;} /// <summary>
/// 为空,无用
/// </summary>
public String date{get;set;} /// <summary>
/// 星期几
/// </summary>
public String week{get;set;} /// <summary>
/// 系统更新时间
/// </summary>
public String fchh{get;set;} /// <summary>
/// 城市代码
/// </summary>
public String cityid{get;set;} /// <summary>
/// 今天及之后五天的摄氏温度
/// </summary>
public String temp1{get;set;}
public String temp2{get;set;}
public String temp3{get;set;}
public String temp4{get;set;}
public String temp5{get;set;}
public String temp6{get;set;} /// <summary>
/// 今天及之后五天的华氏温度
/// </summary>
public String tempF1{get;set;}
public String tempF2{get;set;}
public String tempF3{get;set;}
public String tempF4{get;set;}
public String tempF5{get;set;}
public String tempF6{get;set;} /// <summary>
/// 今天及之后五天的天气描述
/// </summary>
public String weather1{get;set;}
public String weather2{get;set;}
public String weather3{get;set;}
public String weather4{get;set;}
public String weather5{get;set;}
public String weather6{get;set;} /// <summary>
/// 天气描述图片序号
/// </summary>
public String img1{get;set;}
public String img2{get;set;}
public String img3{get;set;}
public String img4{get;set;}
public String img5{get;set;}
public String img6{get;set;}
public String img7{get;set;}
public String img8{get;set;}
public String img9{get;set;}
public String img10{get;set;}
public String img11{get;set;}
public String img12{get;set;}
public String img_single{get;set;} /// <summary>
/// 图片名称
/// </summary>
public String img_title1{get;set;}
public String img_title2{get;set;}
public String img_title3{get;set;}
public String img_title4{get;set;}
public String img_title5{get;set;}
public String img_title6{get;set;}
public String img_title7{get;set;}
public String img_title8{get;set;}
public String img_title9{get;set;}
public String img_title10{get;set;}
public String img_title11{get;set;}
public String img_title12{get;set;}
public String img_title_single{get;set;} /// <summary>
/// 今天及之后五天的风速描述
/// </summary>
public String wind1{get;set;}
public String wind2{get;set;}
public String wind3{get;set;}
public String wind4{get;set;}
public String wind5{get;set;}
public String wind6{get;set;} /// <summary>
/// 风速级别描述
/// </summary>
public String fx1{get;set;}
public String fx2{get;set;}
public String fl1{get;set;}
public String fl2{get;set;}
public String fl3{get;set;}
public String fl5{get;set;}
public String fl6{get;set;} /// <summary>
/// 今天穿衣指数
/// </summary>
public String index{get;set;}
public String index_d{get;set;} /// <summary>
/// 48小时穿衣指数
/// </summary>
public String index48{get;set;}
public String index48_d{get;set;} /// <summary>
/// 紫外线及48小时紫外线
/// </summary>
public String index_uv{get;set;}
public String index48_uv{get;set;} /// <summary>
/// 洗车
/// </summary>
public String index_xc{get;set;} /// <summary>
/// 旅游
/// </summary>
public String index_tr{get;set;} /// <summary>
/// 舒适指数
/// </summary>
public String index_co{get;set;} /// <summary>
/// ??
/// </summary>
public String st1{get;set;}
public String st2{get;set;}
public String st3{get;set;}
public String st4{get;set;}
public String st5{get;set;}
public String st6{get;set;} /// <summary>
/// 晨练
/// </summary>
public String index_cl{get;set;} /// <summary>
/// 晾晒
/// </summary>
public String index_ls{ get; set; } /// <summary>
/// 过敏
/// </summary>
public String index_ag{get;set;}
}
C#调用国家气象局天气预报接口

总结:从上面三个接口来看,调用的关键是获取【城市代码】,所以气象局又提供了三个查询城市代码接口。个人有个疑问?为何气象局不提供一个或三个xml文档或json文本?比现在省市县逐级拼接获取方便很多,或许已经有,只是我不知道,求补充啊!

4、省份或直辖市代码接口

地址:http://www.weather.com.cn/data/citydata/china.html

结果:

{"10101":"北京","10102":"上海","10103":"天津","10104":"重庆","10105":"黑龙江","10106":"吉林","10107":"辽宁","10108":"内蒙古","10109":"河北","10110":"山西","10111":"陕西","10112":"山东","10113":"*","10114":"*","10115":"青海","10116":"甘肃","10117":"宁夏","10118":"河南","10119":"江苏","10120":"湖北","10121":"浙江","10122":"安徽","10123":"福建","10124":"江西","10125":"湖南","10126":"贵州","10127":"四川","10128":"广东","10129":"云南","10130":"广西","10131":"海南","10132":"香港","10133":"澳门","10134":"*"}

5、省会或地区市代码接口

地址:http://www.weather.com.cn/data/citydata/district/【省份或直辖市代码】.html

例如:查询福建省包含的城市

http://www.weather.com.cn/data/citydata/district/10123.html

结果:

{"01":"福州","02":"厦门","03":"宁德","04":"莆田","05":"泉州","06":"漳州","07":"龙岩","08":"三明","09":"南平"}

6、区或县代码

地址:http://www.weather.com.cn/data/citydata/city/【省份或直辖市代码】+【省会或地区市代码】.html

例如:查询福建省南平市包含的区县

http://www.weather.com.cn/data/citydata/city/1012309.html

结果:

{"01":"南平","02":"顺昌","03":"光泽","04":"邵武","05":"武夷山","06":"浦城","07":"建阳","08":"松溪","09":"政和","10":"建瓯"}

说明:

注意该结果不仅包含区县,还包含上级的地级市或省会城市,所以我们只要获取这一级别的结果,再拼接就可以获取省会城市、地级市及区县的城市代码!

三. 城市代码拼接

通过上述三个接口,我们可以通过拼接省市县级别城市代码,来获取南平市或政和县的城市代码。

例如:南平市代码为:101230901【10123是福建省的省级代码+09南平市的市级代码+01南平市的区县级代码】,同理,政和县代码为:101230909。

知道城市代码后,要获取政和县的今日天气,调用下面的url就可以了:

http://www.weather.com.cn/data/sk/101230909.html

注意:并不是所有的城市代码都是通过省市县来拼接,存在下面两个问题。个人建议各城市的代码完全可以==该城市的邮编,因为各省市县的邮编都是不可能重复的,but.....

问题一:海南下属市及湖北荆州的沙市中区县代码包含省级代码和市级代码,如:

http://www.weather.com.cn/data/citydata/city/1013101.html

http://www.weather.com.cn/data/citydata/city/1012008.html

问题二:四个直辖市的城市代码不是由省+市+县代码拼接,而是用省+县+市代码拼接方式。

所以,城市代码的拼接方法如下:

C#调用国家气象局天气预报接口
//pID:省或直辖市代码
//cID:省会或地区市代码
//dID:区或县代码
int code=;//城市代码
//name:区县名称
//如果是海南省或湖北荆州沙市【感谢luoliurong反馈,否则未对沙市特别处理,无法查询】,则直接获取
if (pID == ||name == "沙市")
{
code = int.Parse(dID);
}
//如果是直辖市
else if (pID < )
{
int.TryParse(string.Format("{0}{1}{2}", pID, dID, cID), out code);
}
else
{
int.TryParse(string.Format("{0}{1}{2}", pID, cID, dID), out code);
}
C#调用国家气象局天气预报接口

通过上述说明,表明我们需要建一个项目循环请求并获取各级城市的城市代码,可以将城市代码保存在数据库中,也可以保存在xml文件中。我做法是保存在数据库中,同时生成xml文件,一周更新xml文件一次,并将该xml通过文件依赖缓存到内存中,使用该缓存的xml作为城市代码查询的数据源。

四. 省市县代码表表结构

表名:Weather_Province【省代码表实体】

序号

列名

数据类型

长度

标识

主键

允许空

说明

1

ID

int

4

自增ID

2

Name

varchar

20

   

省名称

3

PID

int

4

   

省代码

表名:Weather_City【省会或地级市代码表实体】

序号

列名

数据类型

长度

标识

主键

允许空

说明

1

ID

int

4

自增ID

2

Name

varchar

20

   

城市名称

3

PID

int

4

   

城市所在省ID

4

CID

varchar

20

   

城市所在的市ID【会重复,省ID+市ID才是唯一】

表名:Weather_District【区或县代码表实体】

序号

列名

数据类型

长度

标识

主键

允许空

说明

1

ID

int

4

自增ID

2

Name

varchar

20

   

县名称

3

PID

int

4

   

所在省ID

4

CID

varchar

20

   

上级市ID

5

DID

varchar

20

   

县ID

6

Code

int

4

   

可被直接调用代码,即该区县的代码

源码包中的DBScript.sql为上述数据脚本【点击此处下载】

五. 获取省级代码数据

Http请求方法:

C#调用国家气象局天气预报接口
/// <summary>
/// Get方式获取url地址输出内容
/// </summary>
/// <param name="url">url</param>
/// <param name="encoding">返回内容编码方式,例如:Encoding.UTF8</param>
/// <returns></returns>
public static String SendRequestByGetMethod(String url,Encoding encoding)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding);
return sr.ReadToEnd();
}
C#调用国家气象局天气预报接口

代码【点击此处下载】

C#调用国家气象局天气预报接口
/// <summary>
/// 生成天气查询省代码表
/// </summary>
private void CreateProvinceCode()
{
m_ProvinceBLL.TruncateProvince();
var responseText = HttpRequestOpt.SendRequestByGetMethod("http://www.weather.com.cn/data/citydata/china.html",System.Text.Encoding.UTF8);
if (!String.IsNullOrEmpty(responseText))
{
m_ProvinceBLL.SaveProvinceCode(responseText);
}
}
C#调用国家气象局天气预报接口
C#调用国家气象局天气预报接口
/// <summary>
/// 添加天气查询省代码表
/// </summary>
/// <param name="responseText">查询结果文本</param>
public void SaveProvinceCode(String responseText)
{
responseText = responseText.TrimStart('{').TrimEnd('}');
List<Weather_ProvinceModel> list = new List<Weather_ProvinceModel>();
responseText.Split(',').ToList().ForEach(item =>
{
Weather_ProvinceModel provinceModel = new Weather_ProvinceModel();
if (!String.IsNullOrEmpty(item) && item.Split(':').Count() == && !String.IsNullOrEmpty(item.Split(':')[]) && !String.IsNullOrEmpty(item.Split(':')[]))
{
int pID = ;
int.TryParse(item.Split(':')[].Trim('"'), out pID);
provinceModel.PID = pID;
provinceModel.Name = item.Split(':')[].Trim('"');
list.Add(provinceModel);
}
});
if (list.Count > )
{
m_ProvinceDAL.SaveProvinceCode(list);
}
}
C#调用国家气象局天气预报接口
C#调用国家气象局天气预报接口
/// <summary>
/// 批量添加天气查询省代码表
/// </summary>
/// <param name="list">省代码表实体集</param>
public void SaveProvinceCode(List<Weather_ProvinceModel> list)
{
SqlBulkCopy sqlBc = null;
DataTable dt = new DataTable();
try
{
sqlBc = new SqlBulkCopy(Common.WebConfig.WeatherDBConnString, SqlBulkCopyOptions.UseInternalTransaction);
sqlBc.DestinationTableName = "Weather_Province";
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("PID", typeof(int));
if (dt.Rows.Count > )
{
sqlBc.WriteToServer(dt);
dt.Clear();
}
list.ForEach(entity =>
{
DataRow dr = dt.NewRow();
dr["Name"] = entity.Name;
dr["PID"] = entity.PID;
dt.Rows.Add(dr);
});
if (dt.Rows.Count > )
{
sqlBc.WriteToServer(dt);
dt.Clear();
}
}
catch (Exception ex)
{
string mayLoseData = Common.JsonHelp.SerializeJson(dt);
Common.Log4NetHelp.LoseDataLog("ProvinceDAL", "SaveProvinceCode", string.Format("批量添加天气查询省代码表异常,异常信息:{0}", ex.Message), mayLoseData);
}
finally
{
if (sqlBc != null)
{
sqlBc.Close();
}
}
}
C#调用国家气象局天气预报接口

六. 获取省会或地级市代码表数据

代码【点击此处下载】

C#调用国家气象局天气预报接口
///<summary>/// 获取天气查询省代码表所有数据
///</summary>///<returns></returns>public DataSet GetAllProvince()
{
String strSql

=

"select * from Weather_Province order by PID asc";
return DbHelperSQL.Query(strSql);
}
C#调用国家气象局天气预报接口
C#调用国家气象局天气预报接口
/// <summary>
/// 生成天气查询市代码表
/// </summary>
private void CreateCityCode()
{
m_CityBLL.TruncateCity();
DataSet ds = m_ProvinceBLL.GetAllProvince();
if (ds != null && ds.Tables.Count > & ds.Tables[].Rows.Count > )
{
ds.Tables[].Rows.Cast<DataRow>().ToList().ForEach(dr =>
{
int pID = ;
int.TryParse(dr["PID"].ToString(), out pID);
if (pID > )
{
string url = string.Format("http://www.weather.com.cn/data/citydata/district/{0}.html", pID);
var responseText = HttpRequestOpt.SendRequestByGetMethod(url,System.Text.Encoding.UTF8);
if (!String.IsNullOrEmpty(responseText))
{
m_CityBLL.SaveCityCode(pID, responseText);
}
}
else
{
Log4NetHelp.ErrorLog("GetCode", "CreateCityCode",string.Format("存在PID<=0的数据"));
}
});
}
}
C#调用国家气象局天气预报接口
C#调用国家气象局天气预报接口
/// <summary>
/// 添加天气查询省会或地级市代码
/// </summary>
/// <param name="pID">省ID</param>
/// <param name="responseText">查询结果文本</param>
public void SaveCityCode(int pID, String responseText)
{
responseText = responseText.TrimStart('{').TrimEnd('}');
List<Weather_CityModel> list = new List<Weather_CityModel>();
responseText.Split(',').ToList().ForEach(item =>
{
Weather_CityModel cityModel = new Weather_CityModel();
if (!String.IsNullOrEmpty(item) && item.Split(':').Count() == && !String.IsNullOrEmpty(item.Split(':')[]) && !String.IsNullOrEmpty(item.Split(':')[]))
{
cityModel.Name = item.Split(':')[].Trim('"');
cityModel.PID = pID;
string cID = item.Split(':')[].Trim('"');
cityModel.CID = cID;
list.Add(cityModel);
}
});
if (list.Count > )
{
m_CityDAL.SaveCityCode(list);
}
}
C#调用国家气象局天气预报接口
C#调用国家气象局天气预报接口
/// <summary>
/// 批量添加天气查询省会或地级市代码表
/// </summary>
/// <param name="list">省会或地级市代码表实体集</param>
public void SaveCityCode(List<Weather_CityModel> list)
{
SqlBulkCopy sqlBc = null;
DataTable dt = new DataTable();
try
{
sqlBc = new SqlBulkCopy(Common.WebConfig.WeatherDBConnString, SqlBulkCopyOptions.UseInternalTransaction);
sqlBc.DestinationTableName = "Weather_City";
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("PID", typeof(int));
dt.Columns.Add("CID", typeof(String));
if (dt.Rows.Count > )
{
sqlBc.WriteToServer(dt);
dt.Clear();
}
list.ForEach(entity =>
{
DataRow dr = dt.NewRow();
dr["Name"] = entity.Name;
dr["PID"] = entity.PID;
dr["CID"] = entity.CID;
dt.Rows.Add(dr);
});
if (dt.Rows.Count > )
{
sqlBc.WriteToServer(dt);
dt.Clear();
}
}
catch (Exception ex)
{
string mayLoseData = Common.JsonHelp.SerializeJson(dt);
Common.Log4NetHelp.LoseDataLog("CityDAL", "SaveCityCode", string.Format("批量添加天气查询省会或地级市代码表异常,异常信息:{0}",ex.Message),mayLoseData);
}
finally
{
if (sqlBc != null)
{
sqlBc.Close();
}
}
}
C#调用国家气象局天气预报接口

七. 获取区或县代码数据

代码【点击此处下载】

C#调用国家气象局天气预报接口
/// <summary>
/// 获取省会或地级市代码表所有数据
/// </summary>
/// <returns></returns>
public DataSet GetAllCity()
{
String strSql = "select * from Weather_City order by PID asc,CID asc";
return DbHelperSQL.Query(strSql);
}
C#调用国家气象局天气预报接口
/// <summary>
/// 生成天气查询区县代码表
/// </summary>
private void CreateDistrictCode()
{
m_DistrictBLL.TruncateDistrict();
DataSet ds = m_CityBLL.GetAllCity();
if (ds != null && ds.Tables.Count > & ds.Tables[].Rows.Count > )
{
ds.Tables[].Rows.Cast<DataRow>().ToList().ForEach(dr =>
{
int pID = ;
int.TryParse(dr["PID"].ToString(), out pID);
String cID = dr["CID"].ToString();
if (pID > &&!string.IsNullOrEmpty(cID))
{
string url = string.Format("http://www.weather.com.cn/data/citydata/city/{0}{1}.html", pID, cID);
var responseText = HttpRequestOpt.SendRequestByGetMethod(url,System.Text.Encoding.UTF8);
if (!String.IsNullOrEmpty(responseText))
{
m_DistrictBLL.SaveDistrictCode(pID,cID,responseText);
}
}
else
{
Log4NetHelp.ErrorLog("GetCode", "CreateDistrictCode", string.Format("Weather_City表存在非法数据ID={0} PID={0} CID={1}", dr["ID"].ToString(), pID.ToString(),cID));
}
});
}
}
/// <summary>
/// 添加天气查询区县代码表
/// </summary>
/// <param name="pID">省ID</param>
/// <param name="cID">省会或地区市ID</param>
/// <param name="responseText">查询结果文本</param>
public void SaveDistrictCode(int pID, String cID, String responseText)
{
responseText = responseText.TrimStart('{').TrimEnd('}');
List<Weather_DistrictModel> list = new List<Weather_DistrictModel>();
responseText.Split(',').ToList().ForEach(item =>
{
Weather_DistrictModel districtModel = new Weather_DistrictModel();
if (!String.IsNullOrEmpty(item) && item.Split(':').Count() == && !String.IsNullOrEmpty(item.Split(':')[]) && !String.IsNullOrEmpty(item.Split(':')[]))
{
string name = item.Split(':')[].Trim('"');
districtModel.Name = name;
districtModel.PID = pID;
districtModel.CID = cID;
var dID=item.Split(':')[].Trim('"');
districtModel.DID = dID;
int code = ;
//海南的DID=Code,无语啊,对比地址:
//http://www.weather.com.cn/data/citydata/city/1013101.html
//http://www.weather.com.cn/data/citydata/city/1011101.html
// 湖北荆州的沙市DID=Code
//http://www.weather.com.cn/data/citydata/city/1012008.html
if (pID == || name == "沙市")
{
code = int.Parse(dID);
}
else if (pID < )
{
//注意:直辖市所属的区县市代码拼接方式与非直辖市不通
int.TryParse(String.Format("{0}{1}{2}", pID.ToString(), dID, cID), out code);
}
else
{
int.TryParse(String.Format("{0}{1}{2}", pID.ToString(), cID, dID), out code);
}
districtModel.Code = code;
list.Add(districtModel);
}
});
if (list.Count > )
{
m_DistrictDAL.SaveDistrictCode(list);
}
}
C#调用国家气象局天气预报接口
/// <summary>
/// 批量添加天气查询区县代码表
/// </summary>
/// <param name="list">区县码表实体集</param>
public void SaveDistrictCode(List<Weather_DistrictModel> list)
{
SqlBulkCopy sqlBc = null;
DataTable dt = new DataTable();
try
{
sqlBc = new SqlBulkCopy(Common.WebConfig.WeatherDBConnString, SqlBulkCopyOptions.UseInternalTransaction);
sqlBc.DestinationTableName = "Weather_District";
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("PID", typeof(int));
dt.Columns.Add("CID", typeof(String));
dt.Columns.Add("DID", typeof(String));
dt.Columns.Add("Code", typeof(int));
if (dt.Rows.Count > )
{
sqlBc.WriteToServer(dt);
dt.Clear();
}
list.ForEach(entity =>
{
DataRow dr = dt.NewRow();
dr["Name"] = entity.Name;
dr["PID"] = entity.PID;
dr["CID"] = entity.CID;
dr["DID"] = entity.DID;
dr["Code"] = entity.Code;
dt.Rows.Add(dr);
});
if (dt.Rows.Count > )
{
sqlBc.WriteToServer(dt);
dt.Clear();
}
}
catch (Exception ex)
{
string mayLoseData = Common.JsonHelp.SerializeJson(dt);
Common.Log4NetHelp.LoseDataLog("DistrictDAL", "SaveDistrictCode", string.Format("批量添加天气查询区县代码表异常,异常信息:{0}", ex.Message), mayLoseData);
}
finally
{
if (sqlBc != null)
{
sqlBc.Close();
}
}
}
C#调用国家气象局天气预报接口

八. 生成天气查询城市代码XML

代码【点击此处下载】

C#调用国家气象局天气预报接口
/// <summary>
/// 获取直辖市、省会、地级市及区县的天气查询代码及市名称+区县名称的全称
/// </summary>
/// <returns></returns>
public DataSet GetNameCodeInfo()
{
string strSql = @"select d.Name as Name,p.Name+d.Name as FullName,d.Code as Code from Weather_District d
left join Weather_Province p on d.PID=p.PID";
return DBUtility.DbHelperSQL.Query(strSql);
}
C#调用国家气象局天气预报接口

说明:为何要全称FullName,因为存在两个城市同名的情况,例如:北京有朝阳,辽宁也有朝阳,所以通过全称来提示用户录入更准确的城市信息,如下:

C#调用国家气象局天气预报接口C#调用国家气象局天气预报接口

C#调用国家气象局天气预报接口
private static String savePath = string.Format("{0}config/weatherConfig.xml", AppDomain.CurrentDomain.BaseDirectory);
/// <summary>
/// 生成xml文档
/// </summary>
public void CreateWeatherXML()
{
var ds = GetNameCodeInfo();
if (ds != null && ds.Tables.Count > && ds.Tables[].Rows.Count > )
{
XElement root = new XElement("Weather");
ds.Tables[].Rows.Cast<DataRow>().ToList().ForEach(dr =>
{
root.Add(new XElement("City",
new XAttribute("Name", dr["Name"].ToString()),
new XAttribute("FullName", dr["FullName"].ToString()),
new XAttribute("Code", dr["Code"].ToString())
));
});
root.Save(savePath);
}
}
C#调用国家气象局天气预报接口

生成天气查询城市代码的xml内容如下:

C#调用国家气象局天气预报接口
<?xml version="1.0" encoding="utf-8"?>
<Weather>
<City Name="北京" FullName="北京北京" Code="101010100" />
<City Name="海淀" FullName="北京海淀" Code="101010200" />
<City Name="朝阳" FullName="北京朝阳" Code="101010300" />
<City Name="顺义" FullName="北京顺义" Code="101010400" />
<City Name="怀柔" FullName="北京怀柔" Code="101010500" />
<City Name="通州" FullName="北京通州" Code="101010600" />
<City Name="昌平" FullName="北京昌平" Code="101010700" />
<City Name="延庆" FullName="北京延庆" Code="101010800" />
<City Name="丰台" FullName="北京丰台" Code="101010900" />
<City Name="石景山" FullName="北京石景山" Code="101011000" />
<City Name="大兴" FullName="北京大兴" Code="101011100" />
<City Name="房山" FullName="北京房山" Code="101011200" />
<City Name="密云" FullName="北京密云" Code="101011300" />
<City Name="门头沟" FullName="北京门头沟" Code="101011400" />
<City Name="平谷" FullName="北京平谷" Code="101011500" />
<City Name="上海" FullName="上海上海" Code="101020100" />
<City Name="闵行" FullName="上海闵行" Code="101020200" />
<City Name="宝山" FullName="上海宝山" Code="101020300" />
<City Name="嘉定" FullName="上海嘉定" Code="101020500" />
<City Name="南汇" FullName="上海南汇" Code="101020600" />
<City Name="金山" FullName="上海金山" Code="101020700" />
<City Name="青浦" FullName="上海青浦" Code="101020800" />
<City Name="松江" FullName="上海松江" Code="101020900" />
<City Name="奉贤" FullName="上海奉贤" Code="101021000" />
<City Name="崇明" FullName="上海崇明" Code="101021100" />
<City Name="徐家汇" FullName="上海徐家汇" Code="101021200" />
<City Name="浦东" FullName="上海浦东" Code="101021300" />
<City Name="天津" FullName="天津天津" Code="101030100" />
<City Name="武清" FullName="天津武清" Code="101030200" />
<City Name="宝坻" FullName="天津宝坻" Code="101030300" />
<City Name="东丽" FullName="天津东丽" Code="101030400" />
<City Name="西青" FullName="天津西青" Code="101030500" />
<City Name="北辰" FullName="天津北辰" Code="101030600" />
<City Name="宁河" FullName="天津宁河" Code="101030700" />
<City Name="汉沽" FullName="天津汉沽" Code="101030800" />
<City Name="静海" FullName="天津静海" Code="101030900" />
<City Name="津南" FullName="天津津南" Code="101031000" />
<City Name="塘沽" FullName="天津塘沽" Code="101031100" />
<City Name="大港" FullName="天津大港" Code="101031200" />
<City Name="蓟县" FullName="天津蓟县" Code="101031400" />
<City Name="重庆" FullName="重庆重庆" Code="101040100" />
<City Name="永川" FullName="重庆永川" Code="101040200" />
<City Name="合川" FullName="重庆合川" Code="101040300" />
<City Name="南川" FullName="重庆南川" Code="101040400" />
<City Name="江津" FullName="重庆江津" Code="101040500" />
<City Name="万盛" FullName="重庆万盛" Code="101040600" />
<City Name="渝北" FullName="重庆渝北" Code="101040700" />
<City Name="北碚" FullName="重庆北碚" Code="101040800" />
<City Name="巴南" FullName="重庆巴南" Code="101040900" />
<City Name="长寿" FullName="重庆长寿" Code="101041000" />
<City Name="黔江" FullName="重庆黔江" Code="101041100" />
<City Name="万州" FullName="重庆万州" Code="101041300" />
<City Name="涪陵" FullName="重庆涪陵" Code="101041400" />
<City Name="开县" FullName="重庆开县" Code="101041500" />
<City Name="城口" FullName="重庆城口" Code="101041600" />
<City Name="云阳" FullName="重庆云阳" Code="101041700" />
<City Name="巫溪" FullName="重庆巫溪" Code="101041800" />
<City Name="奉节" FullName="重庆奉节" Code="101041900" />
<City Name="巫山" FullName="重庆巫山" Code="101042000" />
<City Name="潼南" FullName="重庆潼南" Code="101042100" />
<City Name="垫江" FullName="重庆垫江" Code="101042200" />
<City Name="梁平" FullName="重庆梁平" Code="101042300" />
<City Name="忠县" FullName="重庆忠县" Code="101042400" />
<City Name="石柱" FullName="重庆石柱" Code="101042500" />
<City Name="大足" FullName="重庆大足" Code="101042600" />
<City Name="荣昌" FullName="重庆荣昌" Code="101042700" />
<City Name="铜梁" FullName="重庆铜梁" Code="101042800" />
<City Name="璧山" FullName="重庆璧山" Code="101042900" />
<City Name="丰都" FullName="重庆丰都" Code="101043000" />
<City Name="武隆" FullName="重庆武隆" Code="101043100" />
<City Name="彭水" FullName="重庆彭水" Code="101043200" />
<City Name="綦江" FullName="重庆綦江" Code="101043300" />
<City Name="酉阳" FullName="重庆酉阳" Code="101043400" />
<City Name="秀山" FullName="重庆秀山" Code="101043600" />
<City Name="哈尔滨" FullName="黑龙江哈尔滨" Code="101050101" />
<City Name="双城" FullName="黑龙江双城" Code="101050102" />
<City Name="呼兰" FullName="黑龙江呼兰" Code="101050103" />
<City Name="阿城" FullName="黑龙江阿城" Code="101050104" />
<City Name="宾县" FullName="黑龙江宾县" Code="101050105" />
<City Name="依兰" FullName="黑龙江依兰" Code="101050106" />
<City Name="巴彦" FullName="黑龙江巴彦" Code="101050107" />
<City Name="通河" FullName="黑龙江通河" Code="101050108" />
<City Name="方正" FullName="黑龙江方正" Code="101050109" />
<City Name="延寿" FullName="黑龙江延寿" Code="101050110" />
<City Name="尚志" FullName="黑龙江尚志" Code="101050111" />
<City Name="五常" FullName="黑龙江五常" Code="101050112" />
<City Name="木兰" FullName="黑龙江木兰" Code="101050113" />
<City Name="齐齐哈尔" FullName="黑龙江齐齐哈尔" Code="101050201" />
<City Name="讷河" FullName="黑龙江讷河" Code="101050202" />
<City Name="龙江" FullName="黑龙江龙江" Code="101050203" />
<City Name="甘南" FullName="黑龙江甘南" Code="101050204" />
<City Name="富裕" FullName="黑龙江富裕" Code="101050205" />
<City Name="依安" FullName="黑龙江依安" Code="101050206" />
<City Name="拜泉" FullName="黑龙江拜泉" Code="101050207" />
<City Name="克山" FullName="黑龙江克山" Code="101050208" />
<City Name="克东" FullName="黑龙江克东" Code="101050209" />
<City Name="泰来" FullName="黑龙江泰来" Code="101050210" />
<City Name="牡丹江" FullName="黑龙江牡丹江" Code="101050301" />
<City Name="海林" FullName="黑龙江海林" Code="101050302" />
<City Name="穆棱" FullName="黑龙江穆棱" Code="101050303" />
<City Name="林口" FullName="黑龙江林口" Code="101050304" />
<City Name="绥芬河" FullName="黑龙江绥芬河" Code="101050305" />
<City Name="宁安" FullName="黑龙江宁安" Code="101050306" />
<City Name="东宁" FullName="黑龙江东宁" Code="101050307" />
<City Name="佳木斯" FullName="黑龙江佳木斯" Code="101050401" />
<City Name="汤原" FullName="黑龙江汤原" Code="101050402" />
<City Name="抚远" FullName="黑龙江抚远" Code="101050403" />
<City Name="桦川" FullName="黑龙江桦川" Code="101050404" />
<City Name="桦南" FullName="黑龙江桦南" Code="101050405" />
<City Name="同江" FullName="黑龙江同江" Code="101050406" />
<City Name="富锦" FullName="黑龙江富锦" Code="101050407" />
<City Name="绥化" FullName="黑龙江绥化" Code="101050501" />
<City Name="肇东" FullName="黑龙江肇东" Code="101050502" />
<City Name="安达" FullName="黑龙江安达" Code="101050503" />
<City Name="海伦" FullName="黑龙江海伦" Code="101050504" />
<City Name="明水" FullName="黑龙江明水" Code="101050505" />
<City Name="望奎" FullName="黑龙江望奎" Code="101050506" />
<City Name="兰西" FullName="黑龙江兰西" Code="101050507" />
<City Name="青冈" FullName="黑龙*冈" Code="101050508" />
<City Name="庆安" FullName="黑龙江庆安" Code="101050509" />
<City Name="绥棱" FullName="黑龙江绥棱" Code="101050510" />
<City Name="黑河" FullName="黑龙江黑河" Code="101050601" />
<City Name="嫩江" FullName="黑龙江嫩江" Code="101050602" />
<City Name="孙吴" FullName="黑龙江孙吴" Code="101050603" />
<City Name="逊克" FullName="黑龙江逊克" Code="101050604" />
<City Name="五大连池" FullName="黑龙江五大连池" Code="101050605" />
<City Name="北安" FullName="黑龙江北安" Code="101050606" />
<City Name="大兴安岭" FullName="黑龙江大兴安岭" Code="101050701" />
<City Name="塔河" FullName="黑龙江塔河" Code="101050702" />
<City Name="漠河" FullName="黑龙江漠河" Code="101050703" />
<City Name="呼玛" FullName="黑龙江呼玛" Code="101050704" />
<City Name="呼中" FullName="黑龙江呼中" Code="101050705" />
<City Name="新林" FullName="黑龙江新林" Code="101050706" />
<City Name="加格达奇" FullName="黑龙江加格达奇" Code="101050708" />
<City Name="伊春" FullName="黑龙江伊春" Code="101050801" />
<City Name="乌伊岭" FullName="黑龙江乌伊岭" Code="101050802" />
<City Name="五营" FullName="黑龙江五营" Code="101050803" />
<City Name="铁力" FullName="黑龙江铁力" Code="101050804" />
<City Name="嘉荫" FullName="黑龙江嘉荫" Code="101050805" />
<City Name="大庆" FullName="黑龙江大庆" Code="101050901" />
<City Name="林甸" FullName="黑龙江林甸" Code="101050902" />
<City Name="肇州" FullName="黑龙江肇州" Code="101050903" />
<City Name="肇源" FullName="黑龙江肇源" Code="101050904" />
<City Name="杜尔伯特" FullName="黑龙江杜尔伯特" Code="101050905" />
<City Name="七台河" FullName="黑龙江七台河" Code="101051002" />
<City Name="勃利" FullName="黑龙江勃利" Code="101051003" />
<City Name="鸡西" FullName="黑龙江鸡西" Code="101051101" />
<City Name="虎林" FullName="黑龙江虎林" Code="101051102" />
<City Name="密山" FullName="黑龙江密山" Code="101051103" />
<City Name="鸡东" FullName="黑龙江鸡东" Code="101051104" />
<City Name="鹤岗" FullName="黑龙江鹤岗" Code="101051201" />
<City Name="绥滨" FullName="黑龙江绥滨" Code="101051202" />
<City Name="萝北" FullName="黑龙江萝北" Code="101051203" />
<City Name="双鸭山" FullName="黑龙江双鸭山" Code="101051301" />
<City Name="集贤" FullName="黑龙江集贤" Code="101051302" />
<City Name="宝清" FullName="黑龙江宝清" Code="101051303" />
<City Name="饶河" FullName="黑龙江饶河" Code="101051304" />
<City Name="友谊" FullName="黑龙江友谊" Code="101051305" />
<City Name="长春" FullName="吉林长春" Code="101060101" />
<City Name="农安" FullName="吉林农安" Code="101060102" />
<City Name="德惠" FullName="吉林德惠" Code="101060103" />
<City Name="九台" FullName="吉林九台" Code="101060104" />
<City Name="榆树" FullName="吉林榆树" Code="101060105" />
<City Name="双阳" FullName="吉林双阳" Code="101060106" />
<City Name="吉林" FullName="吉林吉林" Code="101060201" />
<City Name="舒兰" FullName="吉林舒兰" Code="101060202" />
<City Name="永吉" FullName="吉林永吉" Code="101060203" />
<City Name="蛟河" FullName="吉林蛟河" Code="101060204" />
<City Name="磐石" FullName="吉林磐石" Code="101060205" />
<City Name="桦甸" FullName="吉林桦甸" Code="101060206" />
<City Name="延吉" FullName="吉林延吉" Code="101060301" />
<City Name="敦化" FullName="吉林敦化" Code="101060302" />
<City Name="安图" FullName="吉林安图" Code="101060303" />
<City Name="汪清" FullName="吉林汪清" Code="101060304" />
<City Name="和龙" FullName="吉林和龙" Code="101060305" />
<City Name="龙井" FullName="吉林龙井" Code="101060307" />
<City Name="珲春" FullName="吉林珲春" Code="101060308" />
<City Name="图们" FullName="吉林图们" Code="101060309" />
<City Name="四平" FullName="吉林四平" Code="101060401" />
<City Name="双辽" FullName="吉林双辽" Code="101060402" />
<City Name="梨树" FullName="吉林梨树" Code="101060403" />
<City Name="公主岭" FullName="吉林公主岭" Code="101060404" />
<City Name="伊通" FullName="吉林伊通" Code="101060405" />
<City Name="通化" FullName="吉林通化" Code="101060501" />
<City Name="梅河口" FullName="吉林梅河口" Code="101060502" />
<City Name="柳河" FullName="吉林柳河" Code="101060503" />
<City Name="辉南" FullName="吉林辉南" Code="101060504" />
<City Name="集安" FullName="吉林集安" Code="101060505" />
<City Name="通化县" FullName="吉林通化县" Code="101060506" />
<City Name="白城" FullName="吉林白城" Code="101060601" />
<City Name="洮南" FullName="吉林洮南" Code="101060602" />
<City Name="大安" FullName="吉林大安" Code="101060603" />
<City Name="镇赉" FullName="吉林镇赉" Code="101060604" />
<City Name="通榆" FullName="吉林通榆" Code="101060605" />
<City Name="辽源" FullName="吉林辽源" Code="101060701" />
<City Name="东丰" FullName="吉林东丰" Code="101060702" />
<City Name="东辽" FullName="吉林东辽" Code="101060703" />
<City Name="*" FullName="吉林*" Code="101060801" />
<City Name="乾安" FullName="吉林乾安" Code="101060802" />
<City Name="前郭" FullName="吉林前郭" Code="101060803" />
<City Name="长岭" FullName="吉林长岭" Code="101060804" />
<City Name="扶余" FullName="吉林扶余" Code="101060805" />
<City Name="白山" FullName="吉林白山" Code="101060901" />
<City Name="靖宇" FullName="吉林靖宇" Code="101060902" />
<City Name="临江" FullName="吉林临江" Code="101060903" />
<City Name="东岗" FullName="吉林东岗" Code="101060904" />
<City Name="长白" FullName="吉林长白" Code="101060905" />
<City Name="抚松" FullName="吉林抚松" Code="101060906" />
<City Name="江源" FullName="吉林江源" Code="101060907" />
<City Name="沈阳" FullName="辽宁沈阳" Code="101070101" />
<City Name="辽中" FullName="辽宁辽中" Code="101070103" />
<City Name="康平" FullName="辽宁康平" Code="101070104" />
<City Name="法库" FullName="辽宁法库" Code="101070105" />
<City Name="新民" FullName="辽宁新民" Code="101070106" />
<City Name="大连" FullName="辽宁大连" Code="101070201" />
<City Name="瓦房店" FullName="辽宁瓦房店" Code="101070202" />
<City Name="金州" FullName="辽宁金州" Code="101070203" />
<City Name="普兰店" FullName="辽宁普兰店" Code="101070204" />
<City Name="旅顺" FullName="辽宁旅顺" Code="101070205" />
<City Name="长海" FullName="辽宁长海" Code="101070206" />
<City Name="庄河" FullName="辽宁庄河" Code="101070207" />
<City Name="鞍山" FullName="辽宁鞍山" Code="101070301" />
<City Name="台安" FullName="辽宁台安" Code="101070302" />
<City Name="岫岩" FullName="辽宁岫岩" Code="101070303" />
<City Name="海城" FullName="辽宁海城" Code="101070304" />
<City Name="抚顺" FullName="辽宁抚顺" Code="101070401" />
<City Name="新宾" FullName="辽宁新宾" Code="101070402" />
<City Name="清原" FullName="辽宁清原" Code="101070403" />
<City Name="章党" FullName="辽宁章党" Code="101070404" />
<City Name="本溪" FullName="辽宁本溪" Code="101070501" />
<City Name="本溪县" FullName="辽宁本溪县" Code="101070502" />
<City Name="桓仁" FullName="辽宁桓仁" Code="101070504" />
<City Name="丹东" FullName="辽宁丹东" Code="101070601" />
<City Name="凤城" FullName="辽宁凤城" Code="101070602" />
<City Name="宽甸" FullName="辽宁宽甸" Code="101070603" />
<City Name="东港" FullName="辽宁东港" Code="101070604" />
<City Name="锦州" FullName="辽宁锦州" Code="101070701" />
<City Name="凌海" FullName="辽宁凌海" Code="101070702" />
<City Name="义县" FullName="辽宁义县" Code="101070704" />
<City Name="黑山" FullName="辽宁黑山" Code="101070705" />
<City Name="北镇" FullName="辽宁北镇" Code="101070706" />
<City Name="营口" FullName="辽宁营口" Code="101070801" />
<City Name="大石桥" FullName="辽宁大石桥" Code="101070802" />
<City Name="盖州" FullName="辽宁盖州" Code="101070803" />
<City Name="阜新" FullName="辽宁阜新" Code="101070901" />
<City Name="彰武" FullName="辽宁彰武" Code="101070902" />
<City Name="辽阳" FullName="辽宁辽阳" Code="101071001" />
<City Name="辽阳县" FullName="辽宁辽阳县" Code="101071002" />
<City Name="灯塔" FullName="辽宁灯塔" Code="101071003" />
<City Name="弓长岭" FullName="辽宁弓长岭" Code="101071004" />
<City Name="铁岭" FullName="辽宁铁岭" Code="101071101" />
<City Name="开原" FullName="辽宁开原" Code="101071102" />
<City Name="昌图" FullName="辽宁昌图" Code="101071103" />
<City Name="西丰" FullName="辽宁西丰" Code="101071104" />
<City Name="调兵山" FullName="辽宁调兵山" Code="101071105" />
<City Name="朝阳" FullName="辽宁朝阳" Code="101071201" />
<City Name="凌源" FullName="辽宁凌源" Code="101071203" />
<City Name="喀左" FullName="辽宁喀左" Code="101071204" />
<City Name="北票" FullName="辽宁北票" Code="101071205" />
<City Name="建平县" FullName="辽宁建平县" Code="101071207" />
<City Name="盘锦" FullName="辽宁盘锦" Code="101071301" />
<City Name="大洼" FullName="辽宁大洼" Code="101071302" />
<City Name="盘山" FullName="辽宁盘山" Code="101071303" />
<City Name="葫芦岛" FullName="辽宁葫芦岛" Code="101071401" />
<City Name="建昌" FullName="辽宁建昌" Code="101071402" />
<City Name="绥中" FullName="辽宁绥中" Code="101071403" />
<City Name="兴城" FullName="辽宁兴城" Code="101071404" />
<City Name="呼和浩特" FullName="内蒙古呼和浩特" Code="101080101" />
<City Name="土左旗" FullName="内蒙古土左旗" Code="101080102" />
<City Name="托县" FullName="内蒙古托县" Code="101080103" />
<City Name="和林" FullName="内蒙古和林" Code="101080104" />
<City Name="清水河" FullName="内蒙古清水河" Code="101080105" />
<City Name="呼市郊区" FullName="内蒙古呼市郊区" Code="101080106" />
<City Name="武川" FullName="内蒙古武川" Code="101080107" />
<City Name="包头" FullName="内蒙古包头" Code="101080201" />
<City Name="白云鄂博" FullName="内蒙古白云鄂博" Code="101080202" />
<City Name="满都拉" FullName="内蒙古满都拉" Code="101080203" />
<City Name="土右旗" FullName="内蒙古土右旗" Code="101080204" />
<City Name="固阳" FullName="内蒙古固阳" Code="101080205" />
<City Name="达茂旗" FullName="内蒙古达茂旗" Code="101080206" />
<City Name="希拉穆仁" FullName="内蒙古希拉穆仁" Code="101080207" />
<City Name="乌海" FullName="内蒙古乌海" Code="101080301" />
<City Name="集宁" FullName="内蒙古集宁" Code="101080401" />
<City Name="卓资" FullName="内蒙古卓资" Code="101080402" />
<City Name="化德" FullName="内蒙古化德" Code="101080403" />
<City Name="商都" FullName="内蒙古商都" Code="101080404" />
<City Name="兴和" FullName="内蒙古兴和" Code="101080406" />
<City Name="凉城" FullName="内蒙古凉城" Code="101080407" />
<City Name="察右前旗" FullName="内蒙古察右前旗" Code="101080408" />
<City Name="察右中旗" FullName="内蒙古察右中旗" Code="101080409" />
<City Name="察右后旗" FullName="内蒙古察右后旗" Code="101080410" />
<City Name="四子王旗" FullName="内蒙古四子王旗" Code="101080411" />
<City Name="丰镇" FullName="内蒙古丰镇" Code="101080412" />
<City Name="通辽" FullName="内蒙古通辽" Code="101080501" />
<City Name="舍伯吐" FullName="内蒙古舍伯吐" Code="101080502" />
<City Name="科左中旗" FullName="内蒙古科左中旗" Code="101080503" />
<City Name="科左后旗" FullName="内蒙古科左后旗" Code="101080504" />
<City Name="青龙山" FullName="内蒙古青龙山" Code="101080505" />
<City Name="开鲁" FullName="内蒙古开鲁" Code="101080506" />
<City Name="库伦" FullName="内蒙古库伦" Code="101080507" />
<City Name="奈曼" FullName="内蒙古奈曼" Code="101080508" />
<City Name="扎鲁特" FullName="内蒙古扎鲁特" Code="101080509" />
<City Name="高力板" FullName="内蒙古高力板" Code="101080510" />
<City Name="巴雅尔吐胡硕" FullName="内蒙古巴雅尔吐胡硕" Code="101080511" />
<City Name="赤峰" FullName="内蒙古赤峰" Code="101080601" />
<City Name="阿鲁旗" FullName="内蒙古阿鲁旗" Code="101080603" />
<City Name="浩尔吐" FullName="内蒙古浩尔吐" Code="101080604" />
<City Name="巴林左旗" FullName="内蒙古巴林左旗" Code="101080605" />
<City Name="巴林右旗" FullName="内蒙古巴林右旗" Code="101080606" />
<City Name="林西" FullName="内蒙古林西" Code="101080607" />
<City Name="克什克腾" FullName="内蒙古克什克腾" Code="101080608" />
<City Name="翁牛特" FullName="内蒙古翁牛特" Code="101080609" />
<City Name="岗子" FullName="内蒙古岗子" Code="101080610" />
<City Name="喀喇沁" FullName="内蒙古喀喇沁" Code="101080611" />
<City Name="八里罕" FullName="内蒙古八里罕" Code="101080612" />
<City Name="宁城" FullName="内蒙古宁城" Code="101080613" />
<City Name="敖汉" FullName="内蒙古敖汉" Code="101080614" />
<City Name="宝国吐" FullName="内蒙古宝国吐" Code="101080615" />
<City Name="鄂尔多斯" FullName="内蒙古鄂尔多斯" Code="101080701" />
<City Name="达拉特" FullName="内蒙古达拉特" Code="101080703" />
<City Name="准格尔" FullName="内蒙古准格尔" Code="101080704" />
<City Name="鄂前旗" FullName="内蒙古鄂前旗" Code="101080705" />
<City Name="河南" FullName="内蒙古河南" Code="101080706" />
<City Name="伊克乌素" FullName="内蒙古伊克乌素" Code="101080707" />
<City Name="鄂托克" FullName="内蒙古鄂托克" Code="101080708" />
<City Name="杭锦旗" FullName="内蒙古杭锦旗" Code="101080709" />
<City Name="乌审旗" FullName="内蒙古乌审旗" Code="101080710" />
<City Name="伊金霍洛" FullName="内蒙古伊金霍洛" Code="101080711" />
<City Name="乌审召" FullName="内蒙古乌审召" Code="101080712" />
<City Name="东胜" FullName="内蒙古东胜" Code="101080713" />
<City Name="临河" FullName="内蒙古临河" Code="101080801" />
<City Name="五原" FullName="内蒙古五原" Code="101080802" />
<City Name="磴口" FullName="内蒙古磴口" Code="101080803" />
<City Name="乌前旗" FullName="内蒙古乌前旗" Code="101080804" />
<City Name="大佘太" FullName="内蒙古大佘太" Code="101080805" />
<City Name="乌中旗" FullName="内蒙古乌中旗" Code="101080806" />
<City Name="乌后旗" FullName="内蒙古乌后旗" Code="101080807" />
<City Name="海力素" FullName="内蒙古海力素" Code="101080808" />
<City Name="那仁宝力格" FullName="内蒙古那仁宝力格" Code="101080809" />
<City Name="杭锦后旗" FullName="内蒙古杭锦后旗" Code="101080810" />
<City Name="锡林浩特" FullName="内蒙古锡林浩特" Code="101080901" />
<City Name="二连浩特" FullName="内蒙古二连浩特" Code="101080903" />
<City Name="阿巴嘎" FullName="内蒙古阿巴嘎" Code="101080904" />
<City Name="苏左旗" FullName="内蒙古苏左旗" Code="101080906" />
<City Name="苏右旗" FullName="内蒙古苏右旗" Code="101080907" />
<City Name="朱日和" FullName="内蒙古朱日和" Code="101080908" />
<City Name="东乌旗" FullName="内蒙古东乌旗" Code="101080909" />
<City Name="西乌旗" FullName="内蒙古西乌旗" Code="101080910" />
<City Name="太仆寺" FullName="内蒙古太仆寺" Code="101080911" />
<City Name="镶黄旗" FullName="内蒙古镶黄旗" Code="101080912" />
<City Name="正镶白旗" FullName="内蒙古正镶白旗" Code="101080913" />
<City Name="正兰旗" FullName="内蒙古正兰旗" Code="101080914" />
<City Name="多伦" FullName="内蒙古多伦" Code="101080915" />
<City Name="博克图" FullName="内蒙古博克图" Code="101080916" />
<City Name="乌拉盖" FullName="内蒙古乌拉盖" Code="101080917" />
<City Name="呼伦贝尔" FullName="内蒙古呼伦贝尔" Code="101081000" />
<City Name="海拉尔" FullName="内蒙古海拉尔" Code="101081001" />
<City Name="小二沟" FullName="内蒙古小二沟" Code="101081002" />
<City Name="阿荣旗" FullName="内蒙古阿荣旗" Code="101081003" />
<City Name="莫力达瓦" FullName="内蒙古莫力达瓦" Code="101081004" />
<City Name="鄂伦春旗" FullName="内蒙古鄂伦春旗" Code="101081005" />
<City Name="鄂温克旗" FullName="内蒙古鄂温克旗" Code="101081006" />
<City Name="陈旗" FullName="内蒙古陈旗" Code="101081007" />
<City Name="新左旗" FullName="内蒙古新左旗" Code="101081008" />
<City Name="新右旗" FullName="内蒙古新右旗" Code="101081009" />
<City Name="满洲里" FullName="内蒙古满洲里" Code="101081010" />
<City Name="牙克石" FullName="内蒙古牙克石" Code="101081011" />
<City Name="扎兰屯" FullName="内蒙古扎兰屯" Code="101081012" />
<City Name="额尔古纳" FullName="内蒙古额尔古纳" Code="101081014" />
<City Name="根河" FullName="内蒙古根河" Code="101081015" />
<City Name="图里河" FullName="内蒙古图里河" Code="101081016" />
<City Name="乌兰浩特" FullName="内蒙古乌兰浩特" Code="101081101" />
<City Name="阿尔山" FullName="内蒙古阿尔山" Code="101081102" />
<City Name="科右中旗" FullName="内蒙古科右中旗" Code="101081103" />
<City Name="胡尔勒" FullName="内蒙古胡尔勒" Code="101081104" />
<City Name="扎赉特" FullName="内蒙古扎赉特" Code="101081105" />
<City Name="索伦" FullName="内蒙古索伦" Code="101081106" />
<City Name="突泉" FullName="内蒙古突泉" Code="101081107" />
<City Name="霍林郭勒" FullName="内蒙古霍林郭勒" Code="101081108" />
<City Name="科右前旗" FullName="内蒙古科右前旗" Code="101081109" />
<City Name="阿左旗" FullName="内蒙古阿左旗" Code="101081201" />
<City Name="阿右旗" FullName="内蒙古阿右旗" Code="101081202" />
<City Name="额济纳" FullName="内蒙古额济纳" Code="101081203" />
<City Name="拐子湖" FullName="内蒙古拐子湖" Code="101081204" />
<City Name="吉兰太" FullName="内蒙古吉兰太" Code="101081205" />
<City Name="锡林高勒" FullName="内蒙古锡林高勒" Code="101081206" />
<City Name="头道湖" FullName="内蒙古头道湖" Code="101081207" />
<City Name="中泉子" FullName="内蒙古中泉子" Code="101081208" />
<City Name="诺尔公" FullName="内蒙古诺尔公" Code="101081209" />
<City Name="雅布赖" FullName="内蒙古雅布赖" Code="101081210" />
<City Name="乌斯泰" FullName="内蒙古乌斯泰" Code="101081211" />
<City Name="孪井滩" FullName="内蒙古孪井滩" Code="101081212" />
<City Name="石家庄" FullName="河北石家庄" Code="101090101" />
<City Name="井陉" FullName="河北井陉" Code="101090102" />
<City Name="正定" FullName="河北正定" Code="101090103" />
<City Name="栾城" FullName="河北栾城" Code="101090104" />
<City Name="行唐" FullName="河北行唐" Code="101090105" />
<City Name="灵寿" FullName="河北灵寿" Code="101090106" />
<City Name="高邑" FullName="河北高邑" Code="101090107" />
<City Name="深泽" FullName="河北深泽" Code="101090108" />
<City Name="赞皇" FullName="河北赞皇" Code="101090109" />
<City Name="无极" FullName="河北无极" Code="101090110" />
<City Name="平山" FullName="河北平山" Code="101090111" />
<City Name="元氏" FullName="河北元氏" Code="101090112" />
<City Name="赵县" FullName="河北赵县" Code="101090113" />
<City Name="辛集" FullName="河北辛集" Code="101090114" />
<City Name="藁城" FullName="河北藁城" Code="101090115" />
<City Name="晋州" FullName="河北晋州" Code="101090116" />
<City Name="新乐" FullName="河北新乐" Code="101090117" />
<City Name="鹿泉" FullName="河北鹿泉" Code="101090118" />
<City Name="保定" FullName="河北保定" Code="101090201" />
<City Name="满城" FullName="河北满城" Code="101090202" />
<City Name="阜平" FullName="河北阜平" Code="101090203" />
<City Name="徐水" FullName="河北徐水" Code="101090204" />
<City Name="唐县" FullName="河北唐县" Code="101090205" />
<City Name="高阳" FullName="河北高阳" Code="101090206" />
<City Name="容城" FullName="河北容城" Code="101090207" />
<City Name="涞源" FullName="河北涞源" Code="101090209" />
<City Name="望都" FullName="河北望都" Code="101090210" />
<City Name="安新" FullName="河北安新" Code="101090211" />
<City Name="易县" FullName="河北易县" Code="101090212" />
<City Name="曲阳" FullName="河北曲阳" Code="101090214" />
<City Name="蠡县" FullName="河北蠡县" Code="101090215" />
<City Name="顺平" FullName="河北顺平" Code="101090216" />
<City Name="雄县" FullName="河北雄县" Code="101090217" />
<City Name="涿州" FullName="河北涿州" Code="101090218" />
<City Name="定州" FullName="河北定州" Code="101090219" />
<City Name="安国" FullName="河北安国" Code="101090220" />
<City Name="高碑店" FullName="河北高碑店" Code="101090221" />
<City Name="涞水" FullName="河北涞水" Code="101090222" />
<City Name="定兴" FullName="河北定兴" Code="101090223" />
<City Name="清苑" FullName="河北清苑" Code="101090224" />
<City Name="博野" FullName="河北博野" Code="101090225" />
<City Name="张家口" FullName="河北张家口" Code="101090301" />
<City Name="宣化" FullName="河北宣化" Code="101090302" />
<City Name="张北" FullName="河北张北" Code="101090303" />
<City Name="康保" FullName="河北康保" Code="101090304" />
<City Name="沽源" FullName="河北沽源" Code="101090305" />
<City Name="尚义" FullName="河北尚义" Code="101090306" />
<City Name="蔚县" FullName="河北蔚县" Code="101090307" />
<City Name="阳原" FullName="河北阳原" Code="101090308" />
<City Name="怀安" FullName="河北怀安" Code="101090309" />
<City Name="万全" FullName="河北万全" Code="101090310" />
<City Name="怀来" FullName="河北怀来" Code="101090311" />
<City Name="涿鹿" FullName="河北涿鹿" Code="101090312" />
<City Name="赤城" FullName="河北赤城" Code="101090313" />
<City Name="崇礼" FullName="河北崇礼" Code="101090314" />
<City Name="承德" FullName="河北承德" Code="101090402" />
<City Name="承德县" FullName="河北承德县" Code="101090403" />
<City Name="兴隆" FullName="河北兴隆" Code="101090404" />
<City Name="平泉" FullName="河北平泉" Code="101090405" />
<City Name="滦平" FullName="河北滦平" Code="101090406" />
<City Name="隆化" FullName="河北隆化" Code="101090407" />
<City Name="丰宁" FullName="河北丰宁" Code="101090408" />
<City Name="宽城" FullName="河北宽城" Code="101090409" />
<City Name="围场" FullName="河北围场" Code="101090410" />
<City Name="唐山" FullName="河北唐山" Code="101090501" />
<City Name="丰南" FullName="河北丰南" Code="101090502" />
<City Name="丰润" FullName="河北丰润" Code="101090503" />
<City Name="滦县" FullName="河北滦县" Code="101090504" />
<City Name="滦南" FullName="河北滦南" Code="101090505" />
<City Name="乐亭" FullName="河北乐亭" Code="101090506" />
<City Name="迁西" FullName="河北迁西" Code="101090507" />
<City Name="玉田" FullName="河北玉田" Code="101090508" />
<City Name="唐海" FullName="河北唐海" Code="101090509" />
<City Name="遵化" FullName="河北遵化" Code="101090510" />
<City Name="迁安" FullName="河北迁安" Code="101090511" />
<City Name="曹妃甸" FullName="河北曹妃甸" Code="101090512" />
<City Name="廊坊" FullName="河北廊坊" Code="101090601" />
<City Name="固安" FullName="河北固安" Code="101090602" />
<City Name="永清" FullName="河北永清" Code="101090603" />
<City Name="香河" FullName="河北香河" Code="101090604" />
<City Name="大城" FullName="河北大城" Code="101090605" />
<City Name="文安" FullName="河北文安" Code="101090606" />
<City Name="大厂" FullName="河北大厂" Code="101090607" />
<City Name="霸州" FullName="河北霸州" Code="101090608" />
<City Name="三河" FullName="河北三河" Code="101090609" />
<City Name="沧州" FullName="河北沧州" Code="101090701" />
<City Name="青县" FullName="河北青县" Code="101090702" />
<City Name="东光" FullName="河北东光" Code="101090703" />
<City Name="海兴" FullName="河北海兴" Code="101090704" />
<City Name="盐山" FullName="河北盐山" Code="101090705" />
<City Name="肃宁" FullName="河北肃宁" Code="101090706" />
<City Name="南皮" FullName="河北南皮" Code="101090707" />
<City Name="吴桥" FullName="河北吴桥" Code="101090708" />
<City Name="献县" FullName="河北献县" Code="101090709" />
<City Name="孟村" FullName="河北孟村" Code="101090710" />
<City Name="泊头" FullName="河北泊头" Code="101090711" />
<City Name="任丘" FullName="河北任丘" Code="101090712" />
<City Name="黄骅" FullName="河北黄骅" Code="101090713" />
<City Name="河间" FullName="河北河间" Code="101090714" />
<City Name="沧县" FullName="河北沧县" Code="101090716" />
<City Name="衡水" FullName="河北衡水" Code="101090801" />
<City Name="枣强" FullName="河北枣强" Code="101090802" />
<City Name="武邑" FullName="河北武邑" Code="101090803" />
<City Name="武强" FullName="河北武强" Code="101090804" />
<City Name="饶阳" FullName="河北饶阳" Code="101090805" />
<City Name="安平" FullName="河北安平" Code="101090806" />
<City Name="故城" FullName="河北故城" Code="101090807" />
<City Name="景县" FullName="河北景县" Code="101090808" />
<City Name="阜城" FullName="河北阜城" Code="101090809" />
<City Name="冀州" FullName="河北冀州" Code="101090810" />
<City Name="深州" FullName="河北深州" Code="101090811" />
<City Name="邢台" FullName="河北邢台" Code="101090901" />
<City Name="临城" FullName="河北临城" Code="101090902" />
<City Name="内丘" FullName="河北内丘" Code="101090904" />
<City Name="柏乡" FullName="河北柏乡" Code="101090905" />
<City Name="隆尧" FullName="河北隆尧" Code="101090906" />
<City Name="南和" FullName="河北南和" Code="101090907" />
<City Name="宁晋" FullName="河北宁晋" Code="101090908" />
<City Name="巨鹿" FullName="河北巨鹿" Code="101090909" />
<City Name="新河" FullName="河北新河" Code="101090910" />
<City Name="广宗" FullName="河北广宗" Code="101090911" />
<City Name="平乡" FullName="河北平乡" Code="101090912" />
<City Name="威县" FullName="河北威县" Code="101090913" />
<City Name="清河" FullName="河北清河" Code="101090914" />
<City Name="临西" FullName="河北临西" Code="101090915" />
<City Name="南宫" FullName="河北南宫" Code="101090916" />
<City Name="沙河" FullName="河北沙河" Code="101090917" />
<City Name="任县" FullName="河北任县" Code="101090918" />
<City Name="邯郸" FullName="河北邯郸" Code="101091001" />
<City Name="峰峰" FullName="河北峰峰" Code="101091002" />
<City Name="临漳" FullName="河北临漳" Code="101091003" />
<City Name="成安" FullName="河北成安" Code="101091004" />
<City Name="大名" FullName="河北大名" Code="101091005" />
<City Name="涉县" FullName="河北涉县" Code="101091006" />
<City Name="磁县" FullName="河北磁县" Code="101091007" />
<City Name="肥乡" FullName="河北肥乡" Code="101091008" />
<City Name="永年" FullName="河北永年" Code="101091009" />
<City Name="邱县" FullName="河北邱县" Code="101091010" />
<City Name="鸡泽" FullName="河北鸡泽" Code="101091011" />
<City Name="广平" FullName="河北广平" Code="101091012" />
<City Name="馆陶" FullName="河北馆陶" Code="101091013" />
<City Name="魏县" FullName="河北魏县" Code="101091014" />
<City Name="曲周" FullName="河北曲周" Code="101091015" />
<City Name="武安" FullName="河北武安" Code="101091016" />
<City Name="秦皇岛" FullName="河北秦皇岛" Code="101091101" />
<City Name="青龙" FullName="河北青龙" Code="101091102" />
<City Name="昌黎" FullName="河北昌黎" Code="101091103" />
<City Name="抚宁" FullName="河北抚宁" Code="101091104" />
<City Name="卢龙" FullName="河北卢龙" Code="101091105" />
<City Name="北戴河" FullName="河北北戴河" Code="101091106" />
<City Name="太原" FullName="山西太原" Code="101100101" />
<City Name="清徐" FullName="山西清徐" Code="101100102" />
<City Name="阳曲" FullName="山西阳曲" Code="101100103" />
<City Name="娄烦" FullName="山西娄烦" Code="101100104" />
<City Name="古交" FullName="山西古交" Code="101100105" />
<City Name="尖草坪区" FullName="山西尖草坪区" Code="101100106" />
<City Name="小店区" FullName="山西小店区" Code="101100107" />
<City Name="大同" FullName="山西大同" Code="101100201" />
<City Name="阳高" FullName="山西阳高" Code="101100202" />
<City Name="大同县" FullName="山西大同县" Code="101100203" />
<City Name="天镇" FullName="山西天镇" Code="101100204" />
<City Name="广灵" FullName="山西广灵" Code="101100205" />
<City Name="灵丘" FullName="山西灵丘" Code="101100206" />
<City Name="浑源" FullName="山西浑源" Code="101100207" />
<City Name="左云" FullName="山西左云" Code="101100208" />
<City Name="阳泉" FullName="山西阳泉" Code="101100301" />
<City Name="盂县" FullName="山西盂县" Code="101100302" />
<City Name="平定" FullName="山西平定" Code="101100303" />
<City Name="晋中" FullName="山西晋中" Code="101100401" />
<City Name="榆次" FullName="山西榆次" Code="101100402" />
<City Name="榆社" FullName="山西榆社" Code="101100403" />
<City Name="左权" FullName="山西左权" Code="101100404" />
<City Name="和顺" FullName="山西和顺" Code="101100405" />
<City Name="昔阳" FullName="山西昔阳" Code="101100406" />
<City Name="寿阳" FullName="山西寿阳" Code="101100407" />
<City Name="太谷" FullName="山西太谷" Code="101100408" />
<City Name="祁县" FullName="山西祁县" Code="101100409" />
<City Name="平遥" FullName="山西平遥" Code="101100410" />
<City Name="灵石" FullName="山西灵石" Code="101100411" />
<City Name="介休" FullName="山西介休" Code="101100412" />
<City Name="长治" FullName="山西长治" Code="101100501" />
<City Name="黎城" FullName="山西黎城" Code="101100502" />
<City Name="屯留" FullName="山西屯留" Code="101100503" />
<City Name="潞城" FullName="山西潞城" Code="101100504" />
<City Name="襄垣" FullName="山西襄垣" Code="101100505" />
<City Name="平顺" FullName="山西平顺" Code="101100506" />
<City Name="武乡" FullName="山西武乡" Code="101100507" />
<City Name="沁县" FullName="山西沁县" Code="101100508" />
<City Name="长子" FullName="山西长子" Code="101100509" />
<City Name="沁源" FullName="山西沁源" Code="101100510" />
<City Name="壶关" FullName="山西壶关" Code="101100511" />
<City Name="晋城" FullName="山西晋城" Code="101100601" />
<City Name="沁水" FullName="山西沁水" Code="10110....未完,更多数据请点击这里下载
C#调用国家气象局天气预报接口

九. 天气查询

代码【点击此处下载】

C#调用国家气象局天气预报接口
/// <summary>
/// weatherConfig.xml文档
/// </summary>
private static XElement docRoot
{
get
{
try
{
string key = "WeatherBLL_docRoot";
var cache = WebCache.GetCache(key);
if (cache != null)
{
return (XElement)cache;
}
else
{
AsynReCreateCodeAndXml();
var result = XElement.Load(savePath);
WebCache.SetCache(key, result, DateTime.Now.AddDays());
return result;
}
}
catch
{
return XElement.Load(savePath);
}
}
} /// <summary>
/// 天气查询
/// </summary>
/// <param name="keyWord">关键字</param>
/// <returns></returns>
public string QueryFutureWeather(String keyWord)
{
keyWord = HandleKeyWord(keyWord);
if (keyWord.Length < )
{
return string.Empty;
}
else
{
var queryResult = from d in docRoot.Elements("City")
where (d.Attribute("Name").Value == keyWord || d.Attribute("FullName").Value == keyWord)
select d;
if (queryResult.Count() > )
{
List<String> fullNames=new List<string>();
foreach(var q in queryResult)
{
fullNames.Add(q.Attribute("FullName").Value);
}
return String.Format("您要查询的是:{0},请输入上述全称之一!", string.Join("、", fullNames.ToArray()));
}
else if (queryResult.Count() == )
{
String code = queryResult.First().Attribute("Code").Value;
try
{
var weatherResult = RequestWeatherResult(code);
return string.Format("今日{0}天气:{1} {2},{3},{4} 洗车指数:{5}。紫外线指数:{6}。晨练指数:{7}.舒适指数:{8}。",
weatherResult.city, weatherResult.temp1, weatherResult.weather1,
weatherResult.wind1, weatherResult.index_d, weatherResult.index_xc,
weatherResult.index_uv, weatherResult.index_cl, weatherResult.index_co);
}
catch (System.Net.WebException ex)
{
Log4NetHelp.ErrorLog("WeatherBLL", "QueryFutureWeather", string.Format("查询天气异常,调用代码为为:{0}", code), ex);
return string.Format("网速繁忙,请您再次查询!");
} }
else
{
return String.Format("抱歉!暂无{0}天气信息", keyWord);
}
}
} /// <summary>
/// 请求天气查询结果
/// </summary>
/// <param name="cityCode">城市代码</param>
/// <returns></returns>
private WeatherinfoModel RequestWeatherResult(String cityCode)
{
String key = string.Format("WeatherBLL_RequestWeatherResult_{0}", cityCode);
var cache= WebCache.GetCache(key);
if (cache != null)
{
return (WeatherinfoModel)cache;
}
else
{
String url = String.Format("http://m.weather.com.cn/data/{0}.html", cityCode);
String result = HttpRequestOpt.SendRequestByGetMethod(url,System.Text.Encoding.UTF8);
result = GetJsonContent(result);
WeatherinfoModel info = (WeatherinfoModel)JsonHelp.DeserializeJson<WeatherinfoModel>(result);
if (info != null)
{
//相同地址查询结果缓存一小时
WebCache.SetCache(key, info, );
}
return info;
}
} /// <summary>
/// 获取天气查询结果里面的JSON内容
/// </summary>
/// <param name="weatherQueryResult">天气查询结果</param>
/// <returns></returns>
private String GetJsonContent(String weatherQueryResult)
{
string pattern = "{\"weatherinfo\":(.*)}";
var result = Regex.Match(weatherQueryResult,pattern,RegexOptions.IgnoreCase).Groups;
if (result.Count > )
{
return result[].Value;
}
return string.Empty;
} /// <summary>
/// 获取包含省市区县关键字的城市
/// </summary>
/// <returns></returns>
private List<String> GetExpectWords()
{
string key = "WeatherBLL_GetExpectWords";
var cache = WebCache.GetCache(key);
if (cache != null)
{
return (List<String>)cache;
}
else
{
List<String> expectionWords = new List<String>();
var ds = m_WeatherDAL.GetExpectWords();
if (ds != null && ds.Tables.Count > && ds.Tables[].Rows.Count > )
{
ds.Tables[].Rows.Cast<DataRow>().ToList().ForEach(dr =>
{
expectionWords.Add(dr["Name"].ToString());
});
}
WebCache.SetCache(key,expectionWords,savePath);
return expectionWords;
}
} /// <summary>
/// 关键字处理,忽略省市区县的关键字,如:北京市>北京
/// </summary>
/// <param name="keyWord">用户录入关键字</param>
/// <returns></returns>
private String HandleKeyWord(String keyWord)
{
List<String> expectWords = GetExpectWords();
if (expectWords.Count > )
{
foreach (String s in expectWords)
{
if (keyWord == s)
{
return keyWord;
}
}
}
List<String> delWords = new List<String>() { "省","市","区","县"};
delWords.ForEach(delWord =>
{
keyWord=keyWord.Replace(delWord, "");
});
return keyWord;
}
C#调用国家气象局天气预报接口
C#调用国家气象局天气预报接口
/// <summary>
/// 获取区县代码表中包含省市区县关键字的城市
/// </summary>
/// <returns></returns>
public DataSet GetExpectWords()
{
string strSql = @"select Name from Weather_District
where (Name like '%省%' or Name like '%市%' or Name like '%区%' or Name like '县')";
return DBUtility.DbHelperSQL.Query(strSql);
}
C#调用国家气象局天气预报接口

十、异步生成城市代码XML

代码【点击此处下载】

C#调用国家气象局天气预报接口
private delegate void ReCreateCodeAndXml();

/// <summary>
/// 异步重新生成城市代码和XML信息
/// </summary>
private static void AsynReCreateCodeAndXml()
{
ReCreateCodeAndXml re = new ReCreateCodeAndXml(ReCreateCodeAndXmlInfo);
IAsyncResult asyn = re.BeginInvoke(new AsyncCallback(CompletedCal), null);
} /// <summary>
/// 重新生成城市代码和XML信息
/// </summary>
private static void ReCreateCodeAndXmlInfo()
{
string url = string.Format("{0}CreateCodeAndXml.ashx", WebConfig.Domain);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Timeout = ;//两分钟
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),Encoding.UTF8);
var result = sr.ReadToEnd();
if (result != "success")
{
Log4NetHelp.ErrorLog("WeatherBLL", "ReCreateCodeAndXmlInfo", string.Format("重新获取省市县代码表及重新生天气xml异常,异常信息:{0}",result));
}
} private static void CompletedCal(IAsyncResult isa)
{ }
C#调用国家气象局天气预报接口

用途:异步更新XML,第一次生成XML的时候使用同步操作,之后更新该XML使用异步,这样可减少http请求时间。

十一、示例代码

代码结构图

C#调用国家气象局天气预报接口

生成各级代码表及创建xml

C#调用国家气象局天气预报接口

天气查询

C#调用国家气象局天气预报接口

示例源码下载:【点击此处下载】