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