天气预报的webservice+三级无刷新级联菜单客户端(js+xmlhttp实现)--Server side

A. xml file for storing area information.
<?xml version="1.0" encoding="utf-8" ?>
<Areas>
  <area name="
华北地区">
    <provinces>
      <province name="
北京">
        <cities>
          <city>
北京</city>
        </cities>
      </province>
      <province name="
天津">
        <cities>
          <city>
天津</city>
        </cities>
      </province>
    ……
    </provinces>
  </area>
  ……
B. cs code for analyzing area information and weather detail----WeatherGet.cs
 
 

  1/****************************************************************************
  2 * Created By:  zhanqiangz(闲云野鹤)
  3 * Time:        Mar 2005
  4 * Description: A simple weather service implemented with xmlhttp
  5 * **************************************************************************/

  6using System;
  7using System.Data;
  8using System.Configuration;
  9using System.Web;
 10using System.Web.Security;
 11using System.Web.UI;
 12using System.Web.UI.WebControls;
 13using System.Web.UI.WebControls.WebParts;
 14using System.Web.UI.HtmlControls;
 15using System.Xml;
 16using System.Collections;
 17using MSXML2;
 18using System.Text.RegularExpressions;
 19
 20/// <summary>
 21/// Summary description for WeatherGet
 22/// </summary>

 23public class WeatherGet
 24{
 25    public WeatherGet()
 26    {
 27        //
 28        // TODO: Add constructor logic here
 29        //
 30    }

 31    /// <summary>
 32    /// Common function for getting areas,provinces and cities.
 33    /// </summary>
 34    /// <param name="strErea"></param>
 35    /// <param name="strProvince"></param>
 36    /// <returns>ArrayList including required data</returns>

 37    internal static ArrayList GetNode(string strErea, string strProvince)
 38    {
 39
 40        XmlDocument doc = new XmlDocument();
 41        {
 42            bool blnIsGetCity = false, blnIsGetErea = false;
 43            blnIsGetErea = string.IsNullOrEmpty(strErea);
 44            blnIsGetCity = !string.IsNullOrEmpty(strProvince);
 45
 46            string strPath = string.Empty;
 47            strPath = HttpContext.Current.Server.MapPath("Erea.xml");
 48            doc.Load(strPath);
 49            XmlNode ParentNode = null;
 50
 51            if (blnIsGetErea)
 52                ParentNode = doc.SelectSingleNode("//Areas");
 53            else if (!blnIsGetErea && !blnIsGetCity)
 54                ParentNode = doc.SelectSingleNode("//area[@name='" + strErea + "']/provinces");
 55            else if (!blnIsGetErea && blnIsGetCity)
 56                ParentNode = doc.SelectSingleNode("//area[@name='" + strErea + "']/provinces/province[@name='" + strProvince + "']/cities");
 57
 58            XmlNodeList list = ParentNode.ChildNodes;
 59            ArrayList result = new ArrayList();
 60            XmlNode node = null;
 61            for (int i = 0; i < list.Count; i++)
 62            {
 63                node = list[i];
 64                if (blnIsGetCity)
 65                    result.Add(node.InnerText);
 66                else
 67                    result.Add(node.Attributes["name"].InnerText);
 68            }

 69            return result;
 70        }

 71    }

 72    /// <summary>
 73    /// Get weather
 74    /// </summary>
 75    /// <param name="Erea"></param>
 76    /// <param name="Province"></param>
 77    /// <param name="City"></param>
 78    /// <returns></returns>

 79    internal static WeatherInfo GetWeather(string Erea, string Province, string City)
 80    {
 81        string strUrl = string.Empty;
 82        string strDay = string.Empty;
 83        string strSunRise = string.Empty;
 84        string strSunSet = string.Empty;
 85        string strDegree = string.Empty;
 86        string strWeather = string.Empty;
 87
 88        WeatherInfo weather = new WeatherInfo();
 89        strUrl = "http://www.cma.gov.cn/netcenter_news/qxyb/city/index.php?city=" + HttpContext.Current.Server.HtmlEncode(City) + "&province=" + HttpContext.Current.Server.HtmlEncode(Province) + "&area=" + HttpContext.Current.Server.HtmlEncode(Erea);
 90        MSXML2.XMLHTTP xmlhttp = new MSXML2.XMLHTTP();
 91        xmlhttp.open("GET", strUrl, falsenullnull);
 92        xmlhttp.send("");
 93        MSXML2.XMLDocument dom = new XMLDocument();
 94
 95        string strSource = xmlhttp.responseText;
 96        strSource = strSource.Substring(strSource.IndexOf("<!--天气预报开始-->"));
 97        strSource = strSource.Substring(0, strSource.IndexOf("<!--未来天气预报结束-->"));
 98        Regex reg = new Regex(@"<B>", RegexOptions.IgnoreCase);
 99        strSource = reg.Replace(strSource, string.Empty);
100        weather.City = City;
101        strDay = AnylysisWeatherInfo(strSource, @"td align=center class=link15>.*?</td>"false);
102        weather.Day = strDay;
103        strSunRise = AnylysisWeatherInfo(strSource, @"日出:.*?</b>"true);
104        weather.SunRiseTime = strSunRise;
105
106        strSunSet = AnylysisWeatherInfo(strSource, @"日落:.*?</b>"true);
107        weather.SunSetTime = strSunSet;
108        strDegree = AnylysisWeatherInfo(strSource, @"td colspan=2 valign=middle align=center class=link12 height=35>.*?</td>"false);
109        weather.Degree = strDegree;
110        strWeather = AnylysisWeatherInfo(strSource, @"td colspan=2 valign=middle align=center class=link17 height=35>.*?</td>"false);
111        weather.Weather = strWeather;
112
113        return weather;
114
115    }

116    /// <summary>
117    /// Parse weather information with regularexpressions.
118    /// </summary>
119    /// <param name="strSourceString"></param>
120    /// <param name="strPattern"></param>
121    /// <param name="fromBeginning"></param>
122    /// <returns></returns>

123    private static string AnylysisWeatherInfo(string strSourceString, string strPattern, bool fromBeginning)
124    {
125        Match m = null;
126        string strResult;
127        int intStart = 0, intLengh = 0;
128        m = Regex.Match(strSourceString, strPattern, RegexOptions.Singleline);
129        strResult = m.Value;
130        if (fromBeginning)
131            strResult = strResult.Substring(0, strResult.IndexOf("<"));
132        else
133        {
134            intStart = strResult.IndexOf(">");
135            intStart += 1;
136            intLengh = strResult.IndexOf("<"- intStart;
137            strResult = strResult.Substring(intStart, intLengh);
138
139        }

140        return strResult;
141    }

142
143}

144/// <summary>
145/// Weather entity.
146/// </summary>

147public class WeatherInfo
148{
149    private string m_City;
150    private string m_SunRiseTime;
151    private string m_SunSetTime;
152    private string m_Weather;
153    private string m_Degree;
154    private string m_Day;
155
156    public string City
157    {
158        set { m_City = value; }
159        get return m_City; }
160    }

161    public string SunRiseTime
162    {
163        set { m_SunRiseTime = value; }
164        get return m_SunRiseTime; }
165    }

166
167    public string SunSetTime
168    {
169        set { m_SunSetTime = value; }
170        get return m_SunSetTime; }
171    }

172
173    public string Weather
174    {
175        set { m_Weather = value; }
176        get return m_Weather; }
177    }

178
179    public string Degree
180    {
181        set { m_Degree = value; }
182        get return m_Degree; }
183    }

184
185    public string Day
186    {
187        set { m_Day = value; }
188        get return m_Day; }
189    }

190
191}

192
C. Service.cs & Service.asmx
 1/****************************************************************************
 2 * Created By:  zhanqiangz(闲云野鹤)
 3 * Time:        Mar 2005
 4 * Description: A simple weather service implemented with xmlhttp, just for fun.
 5 * **************************************************************************/

 6using System;
 7using System.Web;
 8using System.Web.Services;
 9using System.Web.Services.Protocols;
10using System.Collections;
11
12[WebService(Namespace = "http://tempuri.org/")]
13[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14public class Service : System.Web.Services.WebService
15{
16    public Service () {
17
18        //Uncomment the following line if using designed components 
19        //InitializeComponent(); 
20    }

21
22   
23    [WebMethod(Description="Get ereas in china")]
24    public string[] GetEreas()
25    {
26        string[] EreaArray = null;
27        ArrayList list = null;
28        list= WeatherGet.GetNode(nullnull);
29        EreaArray = (string[])(list.ToArray(typeof(string)));
30        return EreaArray;
31    }

32    
33    [WebMethod(Description = "Get Provinces from erea provided")]
34    public string[] GetProvinceFromErea(string strErea)
35    {
36        string[] EreaArray = null;
37        ArrayList list = null;
38        list = WeatherGet.GetNode(strErea, null);
39        EreaArray = (string[])(list.ToArray(typeof(string)));
40        return EreaArray;
41    }

42
43    [WebMethod(Description = "Get cities from province provided")]
44    public string[] GetCitiesFromProvince(string strErea,string strProvince)
45    {
46        string[] EreaArray = null;
47        ArrayList list = null;
48        list = WeatherGet.GetNode(strErea, strProvince);
49        EreaArray = (string[])(list.ToArray(typeof(string)));
50        return EreaArray;
51    }

52    [WebMethod(Description = "Get weather ")]
53    public WeatherInfo GetWeather(string Erea,string Province,string City)
54    {
55        return WeatherGet.GetWeather(Erea, Province, City);
56    }

57}

58   

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>
posted @ 2006-03-02 22:16  zhanqiangz(闲云野鹤)  阅读(2671)  评论(1编辑  收藏  举报