c#獲取中國城市天氣編號程式碼
阿新 • • 發佈:2019-02-03
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()); } } }