Rss.cs
上传用户:xtyqhl
上传日期:2022-06-07
资源大小:212k
文件大小:6k
源码类别:

Windows Mobile

开发平台:

Windows_Unix

  1. /****************************************************************
  2.  ** 文件名:Rss.cs
  3.  ** 创建人:李万龙
  4.  ** 联  系:shenlongju@hotmail.com
  5.  ** 日  期:2008-05-05
  6.  ** 描  述:Rss对象操作
  7.  ** 修改人:
  8.  ** 日  期:
  9.  ** 描  述:
  10.  ** 版  本:1.0.0.0
  11.  ** Copyright (c) 2007-2008 3ESoft
  12. ******************************************************************/
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. using System.Xml;
  17. using System.IO;
  18. using System.Xml.Serialization;
  19. using System.Windows.Forms;
  20. namespace _3ESoft.WindowsMobile.RSS
  21. {
  22.     /// <summary>
  23.     /// Rss操作类
  24.     /// </summary>
  25.     public class Rss
  26.     {
  27.         /// <summary>
  28.         /// RSS列表
  29.         /// </summary>
  30.         public List<RssNews> RssNewsList { set; get; }
  31.         /// <summary>
  32.         /// 更新最新新闻
  33.         /// </summary>
  34.         /// <param name="RSSUrl">RSS地址</param>
  35.         internal void UpdateRssNews(string RSSUrl)
  36.         {
  37.             object obj = new object();
  38.             lock (obj)
  39.             {
  40.                 XmlDocument XmlRSS = new XmlDocument();
  41.                 try
  42.                 {
  43.                     XmlRSS.Load(RSSUrl);
  44.                     XmlNodeList NodeList;
  45.                     NodeList = XmlRSS.SelectNodes("//item");
  46.                     RssNewsList = new List<RssNews>();
  47.                    
  48.                     foreach (XmlNode node in NodeList)
  49.                     {
  50.                         if (node["title"] == null)//没有title标记,不是合法的
  51.                             continue;
  52.                         //是否已经存在了,有则跳过
  53.                         string title = node["title"].InnerText;
  54.                         bool having = false;
  55.                         foreach (RssNews news in RssNewsList)
  56.                         {
  57.                             if (news.Title == title)
  58.                             {
  59.                                 having = true;
  60.                                 break;
  61.                             }
  62.                         }
  63.                         if (having)
  64.                             continue;
  65.                         RssNews MyRssNews = new RssNews();
  66.                         #region 获取RSS属性,有待改善
  67.                         
  68.                         MyRssNews.Title = node["title"].InnerText;
  69.                         
  70.                         if (node["author"] != null)
  71.                         {
  72.                             MyRssNews.Author = node["author"].InnerText;
  73.                         }
  74.                         else if (node["dc:creator"]!=null)
  75.                             MyRssNews.Author = node["dc:creator"].InnerText;
  76.                         if (node["category"] != null)
  77.                         {
  78.                             MyRssNews.Category = node["category"].InnerText;
  79.                         }
  80.                       
  81.                         if (node["description"] != null)
  82.                         {
  83.                             MyRssNews.Description = node["description"].InnerText;
  84.                         }
  85.                         else
  86.                         {
  87.                             MyRssNews.Description = MyRssNews.Title;
  88.                         }
  89.                         if (node["link"] != null)
  90.                         {
  91.                             MyRssNews.Link = node["link"].InnerText;
  92.                         }
  93.                         if (node["pubDate"] != null)
  94.                         {
  95.                             MyRssNews.PubDate = Convert.ToDateTime( node["pubDate"].InnerText).ToString("yy-MM-dd hh:mm");
  96.                         }
  97.                         #endregion
  98.                         MyRssNews.IsRead = false;
  99.                         RssNewsList.Add(MyRssNews);
  100.                     }
  101.                     SaveRssNews(Global.FormatUrl(RSSUrl));
  102.                 }
  103.                 catch (Exception e)
  104.                 {
  105.                     MessageBox.Show(e.Message);
  106.                 }
  107.             }
  108.         }
  109.         
  110.         /// <summary>
  111.         /// 保存新闻文件
  112.         /// </summary>
  113.         /// <param name="FilePath"></param>
  114.         internal void SaveRssNews(string FilePath)
  115.         {
  116.             Stream fs = new FileStream(FilePath, FileMode.Create);
  117.             
  118.             try
  119.             {
  120.                 XmlSerializer serializer = new XmlSerializer(typeof(List<RssNews>));
  121.                 serializer.Serialize(fs, RssNewsList);
  122.                 
  123.             }
  124.             catch (XmlException e)
  125.             {
  126.                 MessageBox.Show(e.Message);
  127.             }
  128.             finally
  129.             {
  130.                 fs.Close();
  131.             }
  132.         }
  133.         /// <summary>
  134.         /// 本地读取新闻列表
  135.         /// </summary>
  136.         /// <param name="FilePath"></param>
  137.         internal void GetRssNewsList(string FilePath)
  138.         {
  139.             if (!File.Exists(FilePath))
  140.             {
  141.                 //MessageBox.Show("首次更新");
  142.                 return;
  143.             }
  144.             FileStream fs = new FileStream(FilePath, FileMode.Open);
  145.             try
  146.             {
  147.                 XmlSerializer serializer = new XmlSerializer(typeof(List<RssNews>));
  148.                 RssNewsList = (List<RssNews>)serializer.Deserialize(fs);
  149.                 
  150.             }
  151.             catch (XmlException e)
  152.             {
  153.                 MessageBox.Show(e.Message);
  154.             }
  155.             finally
  156.             {
  157.                 fs.Close();
  158.             }
  159.         }
  160.     }
  161. }