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;
??????? }
??? }