c#获取中国城市天气编号代码
发布时间
阅读量:
阅读量
花了不少时间编写代码。因天气预报接口开发需调用中国省市编码信息, 我便随手编写了部分代码 snippet, 将这些数据存储在我的数据库中。如有需要可稍作留言, 我会将其发送至您的邮箱坐标(注:此处应保持数学公式...原样)。特别提醒各位学习相关技术的朋友注意: 学习计算机相关知识的朋友无需过分担忧, 因此这方面的难度相对较低。
WInForm界面向来显得过于简陋,因而选择直接粘贴代码。(发现该段代码存在缺失现象,当时我进行了精简处理,最终完成工作大约是在深夜时分,总体印象较为深刻,不过对于如何爬取网页、处理xml数据以及存入sqlserver中等操作还算了解即可。其实无需反复研读这些步骤):
using System;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace Study
{
public partial class WebInfoXML : Form
{
public WebInfoXML()
{
InitializeComponent();
}
//获取网页信息
private string GetWebInfo(string url)
{
WebClient client = new WebClient();
byte[] pageData = client.DownloadData(url);
string pageHtml = Encoding.UTF8.GetString(pageData); //注意编码格式
return pageHtml;
}
//从http://flash.weather.com.cn/wmaps/xml/china.xml获取省份信息
private void btn_GetP_Click(object sender, EventArgs e)
{
#region 插入Province表中国省份信息代码
//richTextBox1.Text = "";
//string url = @"http://flash.weather.com.cn/wmaps/xml/china.xml";
//string str = GetWebInfo(url);
//richTextBox1.Text = str;
//XmlDocument xd = new XmlDocument();
加载xml文件流
//xd.LoadXml(str);
节点
//XmlNode xn = xd.DocumentElement;
第一个节点的值
//string s = xn.ChildNodes[0].Attributes.ToString();
根节
//string b = xd.FirstChild.Name;
节点的数量
//int count = xn.ChildNodes.Count;
特定节点的属性
//foreach (XmlNode node in xn.ChildNodes)
//{
// XmlElement xe = (XmlElement)node;
// string name = xe.GetAttribute("quName");
// string pinyin = xe.GetAttribute("pyName");
// try
// {
// Province pro = new Province();
// pro.P_Name = name;
// pro.P_Pinyin = pinyin;
// DataInformationDataContext dc = new DataInformationDataContext();
// dc.Province.InsertOnSubmit(pro);
// dc.SubmitChanges();
// }
// catch (Exception ex)
// {
// //MessageBox.Show(ex.Message); ;
// }
//}
//MessageBox.Show(b);
#endregion
}
//获取市区信息
private void btn_GetM_Click(object sender, EventArgs e)
{
#region 获取所有市区信息存到表Municipality
//DataInformationDataContext dc = new DataInformationDataContext();
//var urlname = from u in dc.Province select u.P_Pinyin;
//foreach (var item in urlname)
//{
// string url = @"http://flash.weather.com.cn/wmaps/xml/" + item.Trim() + ".xml";
// string str = GetWebInfo(url);
// if (!str.Contains("网页无法访问"))
// {
// //读取xml数据流
// XmlDocument xd = new XmlDocument();
// xd.LoadXml(str);
// //节点
// XmlNode xn = xd.DocumentElement;
// //根节点的值
// string root = xd.FirstChild.Name;
// //找到Province表中P_ID的值。
// var pname = from p in dc.Province where p.P_Pinyin == root select p;
// int id = pname.FirstOrDefault().P_ID;
// foreach (XmlNode node in xn.ChildNodes)
// {
// XmlElement xe = (XmlElement)node;
// string name = xe.GetAttribute("cityname");
// string pinyin = xe.GetAttribute("pyName");
// Municipality mu = new Municipality();
// mu.M_Name = name;
// mu.M_Pinyin = pinyin;
// mu.P_ID = id;
// DataInformationDataContext di = new DataInformationDataContext();
// di.Municipality.InsertOnSubmit(mu);
// di.SubmitChanges();
// }
// richTextBox1.Text = id.ToString();
// }
// else
// {
// continue;
// }
//}
//MessageBox.Show("已经完成了");
#endregion
}
//获取县信息
private void btn_GetC_Click(object sender, EventArgs e)
{
#region 将县信息存到数据库中
DataInformationDataContext dc = new DataInformationDataContext();
var urlname = from u in dc.Municipality select u.M_Pinyin;
foreach (var item in urlname)
{
string url = @"http://flash.weather.com.cn/wmaps/xml/" + item.Trim() + ".xml";
string str = GetWebInfo(url);
if (!str.Contains("网页无法访问"))
{
//读取xml数据流
XmlDocument xd = new XmlDocument();
xd.LoadXml(str);
//节点
XmlNode xn = xd.DocumentElement;
//根节点的值
string root = xd.FirstChild.Name;
//找到Municipality表中M_ID的值。
var mname = from m in dc.Municipality where m.M_Pinyin == root select m;
int id = mname.FirstOrDefault().M_ID;
foreach (XmlNode node in xn.ChildNodes)
{
XmlElement xe = (XmlElement)node;
string name = xe.GetAttribute("cityname");
string pinyin = xe.GetAttribute("pyName");
County county = new County();
county.M_ID = id;
county.C_Name = name;
county.C_Pinyin = pinyin;
DataInformationDataContext di = new DataInformationDataContext();
di.County.InsertOnSubmit(county);
di.SubmitChanges();
}
//richTextBox1.Text = id.ToString();
}
else
{
continue;
}
}
MessageBox.Show("完成了!");
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
//string path = @"E:\C#_Code\Winform\Study\Study\XML\WeatherInfo.xml";
//XmlDocument xd = new XmlDocument();
//xd.Load(path);
//XmlNode xn = xd.DocumentElement;
存到Province表中
//XmlNodeList no = xd.GetElementsByTagName("province");
//foreach (XmlNode item in no)
//{
// string id = item.Attributes["id"].Value;
// string name = item.Attributes["name"].Value;
// try
// {
// Provinces pro = new Provinces();
// pro.ID = id;
// pro.Name = name;
// pro.Nation_ID = "1";
// DataAreaDataContext da = new DataAreaDataContext();
// da.Provinces.InsertOnSubmit(pro);
// da.SubmitChanges();
// }
// catch (Exception)
// {
// throw;
// }
// //richTextBox1.Text += id + name + "\n";
//}
//MessageBox.Show(no.Count.ToString());
存到City表中
//XmlNodeList c = xd.GetElementsByTagName("city");
//foreach (XmlNode item in c)
//{
// string id = item.Attributes["id"].Value;
// string name = item.Attributes["name"].Value;
// Citys citys = new Citys();
// citys.ID = id;
// citys.Name = name;
// DataAreaDataContext da = new DataAreaDataContext();
// da.Citys.InsertOnSubmit(citys);
// da.SubmitChanges();
// //richTextBox1.Text += id + name + "\n";
//}
//MessageBox.Show(c.Count.ToString());
存到County表中
//XmlNodeList co = xd.GetElementsByTagName("county");
//foreach (XmlNode item in co)
//{
// string id = item.Attributes["id"].Value;
// string name = item.Attributes["name"].Value;
// string weaId = item.Attributes["weatherCode"].Value;
// Countys cou = new Countys();
// cou.ID = id;
// cou.Name = name;
// cou.WeatherCode = weaId;
// DataAreaDataContext da = new DataAreaDataContext();
// da.Countys.InsertOnSubmit(cou);
// da.SubmitChanges();
//}
//MessageBox.Show(co.Count.ToString());
}
}
}
全部评论 (0)
还没有任何评论哟~
