Weather

public class WeatherModel
??? {
??????? #region 定義成員變量
??????? private string _temperature = "";
??????? private string _weather = "";
??????? private string _wind = "";
??????? private string _city = "";
??????? private DateTime _dateStart = DateTime.Now;
??????? private string _coldPoint = "";
??????? private string _weatherImagePath = "";
??????? private string _weatherUrl = "http://www.soso.com/tb.q?cid=tb.tq";?? //遠程的天氣url頁面地址
??????? #endregion

??????? #region 定義類屬性
??????? /// <summary>
??????? /// 溫度
??????? /// </summary>
??????? public string Temperature
??????? {
??????????? get { return _temperature; }
??????????? set { _temperature = value; }
??????? }

??????? /// <summary>
??????? /// 天氣說明
??????? /// </summary>
??????? public string Weather
??????? {
??????????? get { return _weather; }
??????????? set { _weather = value; }
??????? }

??????? /// <summary>
??????? /// 風力
??????? /// </summary>
??????? public string Wind
??????? {
??????????? get { return _wind; }
??????????? set { _wind = value; }
??????? }

??????? /// <summary>
??????? /// 城市
??????? /// </summary>
??????? public string City
??????? {
??????????? get { return _city; }
??????????? set { _city = value; }
??????? }

??????? /// <summary>
??????? /// 起始日期
??????? /// </summary>
??????? public DateTime DateStart
??????? {
??????????? get { return _dateStart; }
??????????? set { _dateStart = value; }
??????? }

??????? /// <summary>
??????? /// 感冒指數
??????? /// </summary>
??????? public string ColdPoint
??????? {
??????????? get { return _coldPoint; }
??????????? set { _coldPoint = value; }
??????? }

??????? /// <summary>
??????? /// 天氣的圖片路徑
??????? /// </summary>
??????? public string WeatherImagePath
??????? {
??????????? get { return _weatherImagePath; }
??????????? set { _weatherImagePath = value; }
??????? }

??????? /// <summary>
??????? /// 讀取天氣的url地址
??????? /// </summary>
??????? public string WeatherUrl
??????? {
??????????? get { return _weatherUrl; }
??????????? set { _weatherUrl = value; }
??????? }
??????? #endregion

??????? public WeatherModel() { }

??????? /// <summary>
??????? /// 要獲得天氣的城市
??????? /// </summary>
??????? /// <param name="city">城市</param>
??????? public WeatherModel(string city)
??????? {
??????????? City = city;
??????? }
??? }


??? public class WeatherReadData
??? {
??????? #region 定義成員變量
??????? private string _weatherUrl = "http://www.soso.com/tb.q?cid=tb.tq&cin=";?? //遠程的天氣url頁面地址
??????? #endregion

??????? #region 定義類屬性
??????? /// <summary>
??????? /// 讀取天氣的url地址
??????? /// </summary>
??????? public string WeatherUrl
??????? {
??????????? get { return _weatherUrl; }
??????????? set { _weatherUrl = value; }
??????? }
??????? #endregion

??????? /// <summary>
??????? /// 獲得從當天開始到后2天,共3天的天氣情況
??????? /// </summary>
??????? /// <param name="city">城市</param>
??????? public WeatherModel[] ReadThreeDayWeather(string city)
??????? {
??????????? //url的編碼方式:
??????????? //string tmp1 = System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312"));
??????????? //string tmp2 = System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.UTF8);

??????????? WeatherUrl += "&city=" + HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312"));
???????????
??????????? System.Net.WebClient client = new System.Net.WebClient();
??????????? //-------------讀取遠程網頁,將其轉換為流
??????????? Stream resStream = null;
??????????? try
??????????? {
??????????????? resStream = client.OpenRead(WeatherUrl);
??????????? }
??????????? catch { return null; }
??????????? StreamReader streamReader = new StreamReader(resStream, System.Text.Encoding.Default);
??????????? //---------------------

??????????? StringBuilder strXml = new StringBuilder();
??????????? StringBuilder strBody = new StringBuilder();
??????????? bool isSaveXml = false;
??????????? //讀取流數據
??????????? while (!streamReader.EndOfStream)
??????????? {
??????????????? string tmp = streamReader.ReadLine();

??????????????? #region 讀取xml
??????????????? if (tmp.Contains("<description>"))
??????????????? {
??????????????????? isSaveXml = true;
??????????????????? strXml.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
??????????????? }
??????????????? else if (tmp.Contains("</description>"))
??????????????? {
??????????????????? isSaveXml = false;
??????????????????? strXml.Append(tmp);
??????????????? }

??????????????? if (isSaveXml)
??????????????????? strXml.Append(tmp);
??????????????? #endregion

??????????????? #region 讀取body中第1天的天氣的數據
??????????????? if (tmp.Contains("<body>"))
??????????????? {
??????????????????? strBody.Append(tmp);
??????????????? }
??????????????? #endregion
??????????? }

??????????? #region 讀取body中的第一個<ul>
??????????? int ulStart = strBody.ToString().IndexOf("<ul>");
??????????? int ulEnd = strBody.ToString().IndexOf("</ul>") + "</ul>".Length;
??????????? string tmpStr = "";

??????????? try
??????????? {
??????????????? tmpStr = strBody.ToString().Substring(ulStart, ulEnd - ulStart);
??????????? }
??????????? catch{}

??????????? //在第一個</li>之前加個</img>
??????????? int liStart = tmpStr.IndexOf("</li>");
??????????? tmpStr = tmpStr.Insert(liStart, "</img>");

??????????? //在最前面加上xml說明
??????????? tmpStr = tmpStr.Insert(0, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
??????????? #endregion

??????????? XmlDocument xmldocFirstDay = new XmlDocument();
??????????? xmldocFirstDay.LoadXml(tmpStr);

??????????? //將字符串轉換為xml格式
??????????? XmlDocument xmldoc = new XmlDocument();
??????????? xmldoc.LoadXml(strXml.ToString());

??????????? WeatherModel[] modelArr = new WeatherModel[3];
??????????? modelArr[0] = new WeatherModel();
??????????? modelArr[1] = new WeatherModel();
??????????? modelArr[2] = new WeatherModel();

??????????? #region 獲得第1天的天氣圖片以及第1天的感冒指數
??????????? XmlNodeList nodeListFirst = xmldocFirstDay.SelectNodes("/ul/li");
??????????? //獲得圖片
??????????? XmlNodeList imgNode = nodeListFirst.Item(0).ChildNodes;
??????????? modelArr[0].WeatherImagePath = imgNode.Item(0).Attributes["src"].InnerText;

??????????? //獲得感冒指數
??????????? modelArr[0].ColdPoint = nodeListFirst.Item(1).InnerText.Split(":".ToCharArray())[1];
??????????? #endregion

??????????? #region 獲得第2天,第3天的天氣圖片
??????????? string tmpWeather = strBody.ToString();
??????????? int start = tmpWeather.IndexOf("<div class=\"weather\">") + "<div class=\"weather\">".Length;
??????????? int end = 0;
??????????? tmpWeather = tmpWeather.Substring(start);
??????????? //獲得第2天的數據
??????????? start = tmpWeather.IndexOf("<div class=\"weather\">") + "<div class=\"weather\">".Length;
??????????? tmpWeather = tmpWeather.Substring(start);
??????????? //獲得第2天的圖片
??????????? start = tmpWeather.IndexOf("<img src=\"") + "<img src=\"".Length;
??????????? end = tmpWeather.IndexOf(".png") - 6;
??????????? modelArr[1].WeatherImagePath = tmpWeather.Substring(start, end);

??????????? //獲得第3天的數據
??????????? start = tmpWeather.IndexOf("<div class=\"weather\">") + "<div class=\"weather\">".Length;
??????????? tmpWeather = tmpWeather.Substring(start);
??????????? //獲得第3天的圖片
??????????? start = tmpWeather.IndexOf("<img src=\"") + "<img src=\"".Length;
??????????? end = tmpWeather.IndexOf(".png") - 6;
??????????? modelArr[2].WeatherImagePath = tmpWeather.Substring(start, end);
??????????? #endregion

??????????? #region 獲得城市
??????????? XmlNodeList nodeCity = xmldoc.SelectNodes("/description/city");
??????????? modelArr[0].City = nodeCity.Item(0).InnerText;
??????????? modelArr[1].City = nodeCity.Item(0).InnerText;
??????????? modelArr[2].City = nodeCity.Item(0).InnerText;
??????????? #endregion

??????????? #region 日期
??????????? //獲得日期
??????????? XmlNodeList nodeDate = xmldoc.SelectNodes("/description/date");
??????????? modelArr[0].DateStart = Convert.ToDateTime(nodeDate.Item(0).InnerText);
??????????? modelArr[1].DateStart = modelArr[0].DateStart.AddDays(1);
??????????? modelArr[2].DateStart = modelArr[0].DateStart.AddDays(2);
??????????? #endregion

??????????? #region 獲得天氣
??????????? //獲得天氣
??????????? XmlNodeList nodeData = xmldoc.SelectNodes("/description/data");

??????????? if (nodeData != null && nodeData.Count > 0)
??????????? {
??????????????? XmlNodeList weathNodeList = nodeData.Item(0).ChildNodes;?? //獲得data下的所有子節點
??????????????? if (weathNodeList != null && weathNodeList.Count > 0)
??????????????? {
??????????????????? for (int i = 0; i < weathNodeList.Count; i++)
??????????????????? {
??????????????????????? XmlNodeList dayNodeList = weathNodeList.Item(i).ChildNodes;? //獲得today下的所有子節點

??????????????????????? if (dayNodeList != null && dayNodeList.Count > 0)
??????????????????????? {
??????????????????????????? modelArr[i].Temperature = dayNodeList.Item(0).InnerText;
??????????????????????????? modelArr[i].Weather = dayNodeList.Item(1).InnerText;
??????????????????????????? modelArr[i].Wind = dayNodeList.Item(2).InnerText;
??????????????????????? }
??????????????????? }
??????????????? }
??????????? }
??????????? #endregion

??????????? #region 資源釋放
??????????? xmldocFirstDay = null;
??????????? xmldoc = null;
??????????? resStream.Flush();
??????????? resStream.Close();
??????????? resStream.Dispose();
??????????? resStream = null;
??????????? streamReader.Close();
??????????? streamReader.Dispose();
??????????? streamReader = null;
??????????? #endregion

??????????? return modelArr;
??????? }
??? }

調用

public partial class WeatherPage : System.Web.UI.Page
??? {
??????? private string CityName;
??????? private int TimeType;
??????? protected void Page_Load(object sender, EventArgs e)
??????? {
??????????? //城市名稱
??????????? if (!String.IsNullOrEmpty(Request.QueryString["CityName"]))
??????????? {
??????????????? CityName = Request.QueryString["CityName"];
??????????? }
??????????? //時間
??????????? TimeType = WEB.ProceFlow.ValidatorValueManage.GetIntValue(Request.QueryString["TimeType"]);

??????????? if (!IsPostBack)
??????????? {
??????????????? Response.Write(GetWeatherInfo(CityName, TimeType));
??????????????? Response.End();
??????????? }
??????? }

??????? /// <summary>
??????? /// 獲得天氣信息
??????? /// </summary>
??????? /// <param name="CityName">城市名稱</param>
??????? /// <param name="TimeType">返回的天氣日期,0.今天;1,明天;2,后天</param>
??????? /// <returns></returns>
??????? /// Enow.Share.WeatherManage.WeatherModel[] modelArr = read.ReadThreeDayWeather(this.TextBox1.Text);
??????? /// 返回今天,明天,后天的天氣 和TimeType匹配
??????? private string GetWeatherInfo(string CityName, int TimeType)
??????? {
??????????? //Server.UrlEncode("杭州")
??????????? string strTemp = "";
??????????? Enow.Share.WeatherManage.WeatherReadData read = new Enow.Share.WeatherManage.WeatherReadData();
??????????? Enow.Share.WeatherManage.WeatherModel[] modelArr = read.ReadThreeDayWeather(CityName);
??????????? if (modelArr == null)
??????????????? return "";
??????????? Enow.Share.WeatherManage.WeatherModel model = modelArr[TimeType];

??????????? strTemp += string.Format("<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td width='30%' align='center'><img src='{0}' width='40' height='40' /></td><td width='70%' align='left'><strong? class='blue'>{1}:{2}</strong><br />感冒指數:{3}</td></tr></table>",model.WeatherImagePath,model.Weather,model.Temperature,model.ColdPoint);

??????????? return strTemp;
??????? }
??? }

轉載于:https://www.cnblogs.com/lonelyofsoul/archive/2013/01/09/weather.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274391.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274391.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274391.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

線框模型_進行計劃之前:線框和模型

線框模型Before we start developing something, we need a plan about what we’re doing and what is the expected result from the project. Same as developing a website, we need to create a mockup before we start developing (coding) because it will cost so much…

撰寫論文時word使用技巧(轉)

------------------------------------- 1. Word2007 的表格自定義格式額度功能是很實用的&#xff0c;比如論文中需要經常插入表格的話&#xff0c; 可以在“表格設計”那里“修改表格樣式”一次性把默認的表格樣式設置為三線表&#xff0c;這樣&#xff0c; 你以后每次插入的…

工作經驗教訓_在設計工作五年后獲得的經驗教訓

工作經驗教訓This June it has been five years since I graduated from college. Since then I’ve been working as a UX designer for a lot of different companies, including a start-up, an application developer, and two consultancy firms.我從大學畢業已經五年了&a…

Wayland 源碼解析之代碼結構

來源&#xff1a;http://blog.csdn.net/basilc/article/details/8074895 獲取、編譯 Wayland 及其依賴庫可參考 Wayland 官方網站的 Build 指南&#xff1a;http://wayland.freedesktop.org/building.html。 Wayland 實現的代碼組成可以分成以下四部分&#xff1a; 1. Wayland…

中文排版規則_非設計師的5條排版規則

中文排版規則01僅以一種字體開始 (01 Start with only one font) The first tip for non-designers dealing with typography is simple and will make your life much easier: Stop combining different fonts you like individually and try using only one font in your fut…

基本響應性的Web設計測試工具

在重新設計頁面的過程中。要使頁面完全響應的設計&#xff08;這意味著它會重新調整大小根據瀏覽器的尺寸和方向&#xff09;。如iPhone和iPad的移動電話和平板電腦我碰到了一些非常方便的響應設計工具&#xff0c;幫我測試網站在不同的屏幕響應。下面的這些響應的網頁設計工具…

ux設計_聲音建議:設計UX聲音的快速指南

ux設計Mating calls, warning grunts, and supportive coos are some of the sounds heard throughout the animal kingdom. All species use finely-tuned noises to communicate to one another and inform others of an action or behavior. We humans aren’t all that dif…

css3高級和低級樣式屬性先后順序

寫css hack 時&#xff0c;由于hack主要針對的是個別瀏覽器&#xff0c;hack的書寫順序應當是從一般到特殊的寫法。 如&#xff1a; .box { width:200px; height:200px; position:fixed; left:0; top:0; _position:absolute; } 如果顛倒順序&#xff0c;從特殊到一般&#xff0…

sans serif_Sans和Serif相遇可愛

sans serifI first noticed it in this tweet. Exciting upcoming product and snazzy motion work aside, “What a fascinating logotype!”, I exclaimed!我在此推文中首先注意到了它。 我驚呼即將推出的激動人心的產品和令人眼花&#xff0c;亂的動作&#xff0c;“多么迷人…

[ckeditor系列]ckeditor 自己寫的一個簡單的image上傳js 運用iframe的ajax上傳

ckeditor最近修改一個上傳的&#xff0c;原來的Image的上傳插件功能很多&#xff0c;但是自己用&#xff0c;沒有必要&#xff0c;就進行了修改&#xff0c;后來就改成了目前的樣子&#xff0c;根據_samples/api_dialog.html 進行了修改&#xff0c;把頁面里面的調用都進行了修…

sql 避免除0錯誤_設計簡歷時避免這3個常見的UX錯誤

sql 避免除0錯誤重點 (Top highlight)Having a great looking resume on hand is very important when you’re looking for a job. It is your ticket to land the interview that will get you one step closer to that one job you’ve been dreaming of.在找工作時&#xf…

一個網站自動化測試程序的設計與實現

CSDN博客不再經常更新&#xff0c;更多優質文章請來 粉絲聯盟網 FansUnion.cn! (FansUnion) 代碼 下載地址&#xff1a;http://download.csdn.net/detail/fansunion/5018357(免積分) 代碼亮點&#xff1a;可讀性很好&#xff0c;注釋詳盡 背景 工作中&#xff0c;在維護一…

如何編寫數據庫可視化界面_編寫用于數據可視化的替代文本

如何編寫數據庫可視化界面什么是替代文字 (What is Alt Text) Alt text (sometimes called Alt tags or alternative text) are written descriptions added to images that convey the meaning of the visual. Good alt text helps more people understand the content. Assis…

(轉)swc與swf的區別

在Flash Builder中用Actionscript寫的類可以打包成swc或swf&#xff0c; 在Flash CS中制作的元件也可以打包成swc或swf文件&#xff0c; 一個swc或swf文件中可以包含多個類或元件&#xff0c; 每個元件會映射成一個類&#xff0c; 因此&#xff0c;在Flash Builder中的類和在Fl…

js 驗證各種格式類型的正則表達式

<script src"scripts/jquery-1.4.1.js" type"text/javascript"></script> <script language"javascript" type"text/javascript"> /** * 定義驗證各種格式類型的正則表達式對象 */ var Regexs { email: …

reloaddata 跳動_紙跳動像素

reloaddata 跳動I would like to open with a problem.我想開一個問題。 Why are so many designer going straight to pixels?為什么這么多設計師直接使用像素&#xff1f; Over the past few years i’ve witnessed this in my team, my clients and others throughout th…

使用自定義RadioButton和ViewPager實現TabHost效果和帶滑動的頁卡效果。

參考自http://www.apkbus.com/android-86125-1-1.html 這篇文章技術含量一般&#xff0c;大家別見笑。源碼我以測試&#xff0c;在底部可下載。 好了先上效果圖&#xff1a; 以下是實現步驟&#xff1a; 1、準備自定義RadioButton控件的樣式圖片等&#xff0c;就是準…

利益相關者軟件工程_改善開發人員團隊與非技術利益相關者之間交流的方法

利益相關者軟件工程Whether you’re working on a startup or a big company, keeping your stakeholders and non-technical partners engaged and up to date on what the tech team has been building can be hard.無論您是在初創公司還是大公司中工作&#xff0c;都要讓您的…

Hibernate的檢索策略

Hibernate的Session在加載一個Java對象時&#xff0c;可以將與這個對象相關聯的其他Java對象都加載到緩存中&#xff0c;以便程序及時調用。但有些情況下&#xff0c;我們不需要加載太多無用的對象到緩存中&#xff0c;一來這樣會撐爆內存&#xff0c;二來增加了訪問數據庫的次…

響應式網格項目動畫布局_響應式網格及其實際使用方式:常見的UI布局

響應式網格項目動畫布局重點 (Top highlight)第二部分 (Part II) Now that you have a basic understanding of how to use grids, you might be wondering how to apply them to layouts you see on the web. Responsive grids are a method to systematically align your des…