c#获取中国城市天气编号代码
2016-06-22 15:37
447 查看
代码写得有点久了,好像由于天气预报接口需要中国省市编码号,我就一气之下,写了些代码,将中国天气网天气预报中出现的省、市、县爬取下来,数据还在我数据库中,谁想要可以留言,我可以发到他/她邮箱中。(别惹学技术的,尤其是学习生化技术的,比计算机可怕多了。)
winform界面太丑,就不传图了,直接贴代码(代码好像不全,当时我删删减减,好像搞到很晚才睡,印象比较深刻,不过理解如何爬取网页,如何处理xml数据,如何存到sqlserver中就行了。其实是我懒得再读一遍代码了):
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()); } } }
相关文章推荐
- 10 在C#中读取文件
- C# 实现二维数组的排序算法(代码)
- c# 字符串前加@
- C# 生成PDF并下载。
- c#控制台正则匹配
- C#控制台使用timer
- C#复习⑨(附带C#参考答案仅限参考)
- c#设计模式-单例模式
- C#初始化数组的方法小结
- C# 时间比较大小
- C#Random()函数详解
- C#复习⑧
- c#中控制不能从一个case标签贯穿到另一个case标签
- C#复习⑦
- C#里的SubString和Convert.ToDateTime
- 【C# 计算代码的执行时间】
- C#中split用法实例总结
- c# toolstrip控件怎么把左边的几个小点去掉??
- C#实现DataSet内数据转化为Excel和Word文件的通用类完整实例
- C#复习⑥