读取RSS源的天气预报Web Server实现

在VS2010中完成

1.数据结构

   /// <summary>
    /// 数据成员
    /// </summary>
    public class rss
    {        
         public string Title;
         public List<Item> Items=new List<Item>();         
    }
    public class Item
    {
        public string Title;
        public string Description;
        public string PubDate;
    }
    public class Weather
    {
        public DateTime WeatherDate;
        public DateTime PubDate;
        public string Temp;
        public string Wind;
        public string Cloud;
    }

2.功能实现

  •    读取RSS源,将读到的文档相关标签的内容存入类rss中,函数ReadXml,具体参考我的另一边博文----C#解析RSS新闻源
  •    将rss数据中的变量存储为天气所需要的特定类型,也即是类Weather,实现代码SetWeather
     public List<Weather> SetWeather(string where)
             {
                 rss rss = new rss();
                 switch (where)
                 { 
                     case "南京":
                         rss = ReadXml("http://weather.raychou.com/?/detail/58238/count_3/rss");
                         break;
                     case "北京":
                         rss = ReadXml("http://weather.raychou.com/?/detail/54511/count_3/rss");
                         break;
                     case "上海":
                         rss = ReadXml("http://weather.raychou.com/?/detail/58367/count_3/rss");
                         break;
                     case "广东":
                         rss = ReadXml("http://weather.raychou.com/?/detail/59287/count_3/rss");
                         break;
                     case "重庆":
                         rss = ReadXml("http://weather.raychou.com/?/detail/57516/count_3/rss");
                         break;
                     case "厦门":
                         rss = ReadXml("http://weather.raychou.com/?/detail/59134/count_3/rss");
                         break;
                     case "杭州":
                         rss = ReadXml("http://weather.raychou.com/?/detail/58457/count_3/rss");
                         break;
                     case "大连":
                         rss = ReadXml("http://weather.raychou.com/?/detail/54662/count_3/rss");
                         break;
                 }
                 List<Weather> weather = new List<Weather>();
                 Weather weather_one = new Weather();
                 for (int i = 0; i < rss.Items.Count; i++)
                 {
                     weather_one.WeatherDate = DateTime.Parse(rss.Items[i].Title);
                     weather_one.PubDate = DateTime.Parse(rss.Items[i].PubDate);
                     string[] sArray = rss.Items[i].Description.Split(' ');
                     weather_one.Cloud=sArray[0].ToString();
                     if (weather_one.Cloud.Contains("雪"))
                         weather_one.Cloud = "雪";
                     else if (weather_one.Cloud.Contains("雨") && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "雨";
                     else if (weather_one.Cloud.Contains("阴") && weather_one.Cloud.Contains("雨") == false && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "阴";
                     else if (weather_one.Cloud.Contains("云") && weather_one.Cloud.Contains("阴") == false && weather_one.Cloud.Contains("雨") == false && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "云";
                     else if (weather_one.Cloud.Contains("晴") && weather_one.Cloud.Contains("云") == false && weather_one.Cloud.Contains("阴") == false && weather_one.Cloud.Contains("雨") == false && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "晴";
                     else
                         weather_one.Cloud = "世界末日到了!";
                     weather_one.Temp=sArray[1].ToString();//情 云 雨 雪 阴
                     weather_one.Wind=sArray[2].ToString();
                     weather.Add(weather_one);
                     
                 } 
                 return weather;
    
             }   

    返回的是一个list<Weather>
  • 最后就可以生成一个简单的天气预报Web Server
  • 完整代码如下
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Xml;
    
    namespace WebApplication1
    {
        /// <summary>
        /// Rss 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        // [System.Web.Script.Services.ScriptService]
        public class Rss : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";            
            }
            
            /*public string GetWind(DateTime date)
            {
                   
            }
            [WebMethod]
            public int GetTemp(DateTime date)
            { 
                 
            }*/
            public rss ReadXml(string uri)
            {
                XmlTextReader Reader = new XmlTextReader(uri);
                // XmlValidatingReader Valid = new XmlValidatingReader(Reader);
                // Valid.ValidationType = ValidationType.None;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Reader);
                XmlNode rssNode = FoundChildNode(xmlDoc, "rss");
                XmlNode channelNode = FoundChildNode(rssNode, "channel");
                rss channel = new rss();
                //channel.Items =  new List<rss.Item>();
                for (int i = 0; i < channelNode.ChildNodes.Count; i++)
                {
                    switch (channelNode.ChildNodes[i].Name)
                    {
                        case "title":
                            {
                                channel.Title = channelNode.ChildNodes[i].InnerText;
                                break;
                            }
                        case "item":
                            {
                                Item item = this.getRssItem(channelNode.ChildNodes[i]);
                                channel.Items.Add(item);
                                break;
                            }
                    }
                }
                return channel;
    
            }
            private XmlNode FoundChildNode(XmlNode Node, string Name)
            {
                XmlNode childlNode = null;
                for (int i = 0; i < Node.ChildNodes.Count; i++)
                {
                    if (Node.ChildNodes[i].Name == Name && Node.ChildNodes[i].ChildNodes.Count > 0)
                    {
                        childlNode = Node.ChildNodes[i];
                        return childlNode;
                    }
                }
                return childlNode;
            }
            private Item getRssItem(XmlNode Node)
            {
                Item item = new Item();
                for (int i = 0; i < Node.ChildNodes.Count; i++)
                {
                    if (Node.ChildNodes[i].Name == "title")
                    {
                        item.Title = Node.ChildNodes[i].InnerText;
                    }
                    else if (Node.ChildNodes[i].Name == "description")
                    {
                        item.Description = Node.ChildNodes[i].InnerText;
                    }
                    else if (Node.ChildNodes[i].Name == "pubDate")
                    {
                        item.PubDate = Node.ChildNodes[i].InnerText;
                    }
                }
                return item;
            }
             [WebMethod]
            public List<Weather> SetWeather(string where)
             {
                 rss rss = new rss();
                 switch (where)
                 { 
                     case "南京":
                         rss = ReadXml("http://weather.raychou.com/?/detail/58238/count_3/rss");
                         break;
                     case "北京":
                         rss = ReadXml("http://weather.raychou.com/?/detail/54511/count_3/rss");
                         break;
                     case "上海":
                         rss = ReadXml("http://weather.raychou.com/?/detail/58367/count_3/rss");
                         break;
                     case "广东":
                         rss = ReadXml("http://weather.raychou.com/?/detail/59287/count_3/rss");
                         break;
                     case "重庆":
                         rss = ReadXml("http://weather.raychou.com/?/detail/57516/count_3/rss");
                         break;
                     case "厦门":
                         rss = ReadXml("http://weather.raychou.com/?/detail/59134/count_3/rss");
                         break;
                     case "杭州":
                         rss = ReadXml("http://weather.raychou.com/?/detail/58457/count_3/rss");
                         break;
                     case "大连":
                         rss = ReadXml("http://weather.raychou.com/?/detail/54662/count_3/rss");
                         break;
                 }
                 List<Weather> weather = new List<Weather>();
                 Weather weather_one = new Weather();
                 for (int i = 0; i < rss.Items.Count; i++)
                 {
                     weather_one.WeatherDate = DateTime.Parse(rss.Items[i].Title);
                     weather_one.PubDate = DateTime.Parse(rss.Items[i].PubDate);
                     string[] sArray = rss.Items[i].Description.Split(' ');
                     weather_one.Cloud=sArray[0].ToString();
                     if (weather_one.Cloud.Contains("雪"))
                         weather_one.Cloud = "雪";
                     else if (weather_one.Cloud.Contains("雨") && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "雨";
                     else if (weather_one.Cloud.Contains("阴") && weather_one.Cloud.Contains("雨") == false && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "阴";
                     else if (weather_one.Cloud.Contains("云") && weather_one.Cloud.Contains("阴") == false && weather_one.Cloud.Contains("雨") == false && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "云";
                     else if (weather_one.Cloud.Contains("晴") && weather_one.Cloud.Contains("云") == false && weather_one.Cloud.Contains("阴") == false && weather_one.Cloud.Contains("雨") == false && weather_one.Cloud.Contains("雪") == false)
                         weather_one.Cloud = "晴";
                     else
                         weather_one.Cloud = "世界末日到了!";
                     weather_one.Temp=sArray[1].ToString();//情 云 雨 雪 阴
                     weather_one.Wind=sArray[2].ToString();
                     weather.Add(weather_one);
                     
                 } 
                 return weather;
    
             }   
            
            
        }
       
        /// <summary>
        /// 数据成员
        /// </summary>
        public class rss
        {        
             public string Title;
             public List<Item> Items=new List<Item>();         
        }
        public class Item
        {
            public string Title;
            public string Description;
            public string PubDate;
        }
        public class Weather
        {
            public DateTime WeatherDate;
            public DateTime PubDate;
            public string Temp;
            public string Wind;
            public string Cloud;
        }
    }
    

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值