Rss.cs
资源名称:MobileRss.rar [点击查看]
上传用户:xtyqhl
上传日期:2022-06-07
资源大小:212k
文件大小:6k
源码类别:
Windows Mobile
开发平台:
Windows_Unix
- /****************************************************************
- ** 文件名:Rss.cs
- ** 创建人:李万龙
- ** 联 系:shenlongju@hotmail.com
- ** 日 期:2008-05-05
- ** 描 述:Rss对象操作
- ** 修改人:
- ** 日 期:
- ** 描 述:
- ** 版 本:1.0.0.0
- ** Copyright (c) 2007-2008 3ESoft
- ******************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- using System.IO;
- using System.Xml.Serialization;
- using System.Windows.Forms;
- namespace _3ESoft.WindowsMobile.RSS
- {
- /// <summary>
- /// Rss操作类
- /// </summary>
- public class Rss
- {
- /// <summary>
- /// RSS列表
- /// </summary>
- public List<RssNews> RssNewsList { set; get; }
- /// <summary>
- /// 更新最新新闻
- /// </summary>
- /// <param name="RSSUrl">RSS地址</param>
- internal void UpdateRssNews(string RSSUrl)
- {
- object obj = new object();
- lock (obj)
- {
- XmlDocument XmlRSS = new XmlDocument();
- try
- {
- XmlRSS.Load(RSSUrl);
- XmlNodeList NodeList;
- NodeList = XmlRSS.SelectNodes("//item");
- RssNewsList = new List<RssNews>();
- foreach (XmlNode node in NodeList)
- {
- if (node["title"] == null)//没有title标记,不是合法的
- continue;
- //是否已经存在了,有则跳过
- string title = node["title"].InnerText;
- bool having = false;
- foreach (RssNews news in RssNewsList)
- {
- if (news.Title == title)
- {
- having = true;
- break;
- }
- }
- if (having)
- continue;
- RssNews MyRssNews = new RssNews();
- #region 获取RSS属性,有待改善
- MyRssNews.Title = node["title"].InnerText;
- if (node["author"] != null)
- {
- MyRssNews.Author = node["author"].InnerText;
- }
- else if (node["dc:creator"]!=null)
- MyRssNews.Author = node["dc:creator"].InnerText;
- if (node["category"] != null)
- {
- MyRssNews.Category = node["category"].InnerText;
- }
- if (node["description"] != null)
- {
- MyRssNews.Description = node["description"].InnerText;
- }
- else
- {
- MyRssNews.Description = MyRssNews.Title;
- }
- if (node["link"] != null)
- {
- MyRssNews.Link = node["link"].InnerText;
- }
- if (node["pubDate"] != null)
- {
- MyRssNews.PubDate = Convert.ToDateTime( node["pubDate"].InnerText).ToString("yy-MM-dd hh:mm");
- }
- #endregion
- MyRssNews.IsRead = false;
- RssNewsList.Add(MyRssNews);
- }
- SaveRssNews(Global.FormatUrl(RSSUrl));
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- }
- }
- /// <summary>
- /// 保存新闻文件
- /// </summary>
- /// <param name="FilePath"></param>
- internal void SaveRssNews(string FilePath)
- {
- Stream fs = new FileStream(FilePath, FileMode.Create);
- try
- {
- XmlSerializer serializer = new XmlSerializer(typeof(List<RssNews>));
- serializer.Serialize(fs, RssNewsList);
- }
- catch (XmlException e)
- {
- MessageBox.Show(e.Message);
- }
- finally
- {
- fs.Close();
- }
- }
- /// <summary>
- /// 本地读取新闻列表
- /// </summary>
- /// <param name="FilePath"></param>
- internal void GetRssNewsList(string FilePath)
- {
- if (!File.Exists(FilePath))
- {
- //MessageBox.Show("首次更新");
- return;
- }
- FileStream fs = new FileStream(FilePath, FileMode.Open);
- try
- {
- XmlSerializer serializer = new XmlSerializer(typeof(List<RssNews>));
- RssNewsList = (List<RssNews>)serializer.Deserialize(fs);
- }
- catch (XmlException e)
- {
- MessageBox.Show(e.Message);
- }
- finally
- {
- fs.Close();
- }
- }
- }
- }