您的位置:知识库 » .NET技术

利用YAHOO公开API做天气预报Web服务

作者: VisualStudio  来源: 博客园  发布时间: 2008-10-14 23:48  阅读: 37219 次  推荐: 1   原文链接   [收藏]  

系列文章导航:

创建一个示例和WebMethod特性解析

WebService特性和数组类型解析

类和结构体解析

利用YAHOO公开API做天气预报Web服务

Webservice 的设计和模式

Remoting和Webservice的区别


 

 

其中最重要的是后面的几行,查询当天和第二天的天气情况,我们要获取的天气信息就在里面,代码如下:

 

     <yweather:forecast day="Tue" date="14 Oct 2008" low="47" high="68" text
=
"Mostly Cloudy" code="28" />
      
<yweather:forecast day="Wed" date="15 Oct 2008" low="44" high="70" text
=
"Partly Cloudy" code="30" />

 

我们所需要用到的Node是/rss/channel/item/yweather: forecast;这个节点里的东西是我们需要的。


知道了这些,我们接下来要做的就是在VS中建立WEB服务,并添加下面的代码:

 

namespace Weather
{
    
/// <summary>
    
/// Weather 的摘要说明
    
/// </summary>

    [WebService(Name = "天气预报Web服务", Namespace = "http://www.h2bbs.com/WebService/Weather.asmx", Description = "利用YAHOO公开的API接口,在Web服务实现公开的天气预报调用服务。")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(
false)]
    
public class Weather : System.Web.Services.WebService
    
{
        [WebMethod(Description 
= "根据城市的名称返回该城市的天气预报情况,只能查询省会和直辖市的天气情况。")]
        
public DataSet GetWeather(string strCityName)
        
{
            
string strXml = "http://xml.weather.yahoo.com/forecastrss?p=CHXX" + CityNameToCityNum(strCityName);

            XmlDocument Weather 
= new XmlDocument();
            Weather.Load(strXml);

            XmlNamespaceManager namespacemanager 
= new
XmlNamespaceManager(Weather.NameTable);
            namespacemanager.AddNamespace(
"yweather",
"http://xml.weather.yahoo.com/ns/rss/1.0");
            XmlNodeList nodes 
= Weather.SelectNodes("/rss/channel/item/yweather:forecast", namespacemanager);

            
//XmlNodeList nodes = Weather.GetElementsByTagName("forecast", "http://xml.weather.yahoo.com/ns/rss/1.0");

            DataSet dstWeather 
= new DataSet();
            DataTable dtblWeather 
= new DataTable("Weather");
            dstWeather.Tables.Add(dtblWeather);

            dstWeather.Tables[
"Weather"].Columns.Add("Date"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Week"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Weather"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Tlow"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Thigh"typeof(string));

            
if (nodes.Count > 0)
            
{
                
foreach (XmlNode node in nodes)
                
{
                    DataRow drowWeather 
= dstWeather.Tables["Weather"].NewRow();

                    drowWeather[
"Date"= EmonthToCmonth(node.SelectSingleNode
(
"@date").Value.ToString());
                    drowWeather[
"Week"= EweekToCweek(node.SelectSingleNode
(
"@day").Value.ToString()) + "(" + node.SelectSingleNode("@day").Value.ToString()
+ ")";
                    drowWeather[
"Weather"= node.SelectSingleNode("@text").Value;
                    drowWeather[
"Tlow"= FToC(int.Parse(node.SelectSingleNode
(
"@low").Value)) + "";
                    drowWeather[
"Thigh"= FToC(int.Parse(node.SelectSingleNode
(
"@high").Value)) + "";

                    dstWeather.Tables[
"Weather"].Rows.Add(drowWeather);
                }


                
return dstWeather;
            }

            
else
            
{
                DataRow drowNone 
= dstWeather.Tables["Weather"].NewRow();
                drowNone[
"Week"= "None";
                drowNone[
"Weather"= "None";
                drowNone[
"Tlow"= "None";
                drowNone[
"Thigh"= "None";

                dstWeather.Tables[
"Weather"].Rows.Add(drowNone);
                
return dstWeather;
            }


            
return dstWeather;
        }


        
/// <summary>
        
/// 从华氏转换成摄氏
        
/// </summary>
        
/// <param name="f">华氏度</param>
        
/// <returns></returns>

        private string FToC(int f)
        
{
            
return Math.Round((f - 32/ 1.81).ToString();
        }


        
/// <summary>
        
/// 从星期英文缩写转汉字
        
/// </summary>
        
/// <param name="strEweek">星期的英文缩写</param>
        
/// <returns></returns>

        private string EweekToCweek(string strEweek)
        
{
            
switch (strEweek)
            
{
                
case "Mon":
                    
return "星期一";
                    
break;
                
case "Tue":
                    
return "星期二";
                    
break;
                
case "Wed":
                    
return "星期三";
                    
break;
                
case "Thu":
                    
return "星期四";
                    
break;
                
case "Fri":
                    
return "星期五";
                    
break;
                
case "Sat":
                    
return "星期六";
                    
break;
                
case "Sun":
                    
return "星期日";
                    
break;
                
default:
                    
return "传参错误";
                    
break;
            }

        }


        
/// <summary>
        
/// 从月英文缩写转汉字
        
/// </summary>
        
/// <param name="strReplace">需要替换的年月日</param>
        
/// <returns></returns>

        private string EmonthToCmonth(string strReplace)
        
{
            
return Convert.ToDateTime(strReplace).ToString("yyyy年MM月dd日");
        }


        
/// <summary>
        
/// 根据城市名称返回城市编号
        
/// </summary>
        
/// <param name="strCityName">城市名称</param>
        
/// <returns></returns>

        private string CityNameToCityNum(string strCityNameToNum)
        
{
            
//中国各个省会和直辖市对应的查询代码
            Hashtable htWeather = new Hashtable();
            htWeather.Add(
"北京""0008");
            htWeather.Add(
"天津""0133");
            htWeather.Add(
"杭州""0044");
            htWeather.Add(
"合肥""0448");
            htWeather.Add(
"上海""0116");
            htWeather.Add(
"福州""0031");
            htWeather.Add(
"重庆""0017");
            htWeather.Add(
"南昌""0097");
            htWeather.Add(
"香港""0049");
            htWeather.Add(
"济南""0064");
            htWeather.Add(
"澳门""0512");
            htWeather.Add(
"郑州""0165");
            htWeather.Add(
"呼和浩特""0249");
            htWeather.Add(
"乌鲁木齐""0135");
            htWeather.Add(
"长沙""0013");
            htWeather.Add(
"银川""0259");
            htWeather.Add(
"广州""0037");
            htWeather.Add(
"拉萨""0080");
            htWeather.Add(
"海口""0502");
            htWeather.Add(
"南宁""0100");
            htWeather.Add(
"成都""0016");
            htWeather.Add(
"石家庄""0122");
            htWeather.Add(
"贵阳""0039");
            htWeather.Add(
"太原""0129");
            htWeather.Add(
"昆明""0076");
            htWeather.Add(
"沈阳""0119");
            htWeather.Add(
"西安""0141");
            htWeather.Add(
"长春""0010");
            htWeather.Add(
"兰州""0079");
            htWeather.Add(
"西宁""0236");
            htWeather.Add(
"南京""0099");

            
object cityNum = htWeather[strCityNameToNum];

            
if (cityNum == null)
            
{
                
return "City not found";
            }

            
else
            
{
                
return cityNum.ToString();
            }

        }

    }

}

 

别忘了"using System.Xml;"哦!

 

1
0

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻