c#编程 RSS2.0解析器代码
最近工作比较忙,加上杂七杂八的一堆事情,博客也开始长草了=_=。手里现在唯一没有停滞的项目就是笔者的网站发布机的开发。这是其中解析rss文件的代码;个人觉得比较实用,所以把单独提出来,和各位一道分享^_^。用法也相当简单,下面的代码中已经包含了控制台的示例代码,有兴趣的朋友可以看看
using System; using System.Xml; using System.Collections.Generic; namespace Freemose.Base.RSS { public interface iXml{ XmlNode FindChildNode(XmlNode node, string name); } public class RssReader :iXml { private XmlDocument doc; /// <summary> /// 文档DOM /// </summary> public XmlDocument Doc { get { return doc; } } private XmlNode rootnode; private List<XmlNode> items; /// <summary> /// 获取channel的相关属性 /// </summary> /// <param name="attribute">指定属性名称</param> /// <returns></returns> public string this[RssChannel attribute] { get { try { XmlNode node = FindChildNode(rootnode, attribute.ToString()); if (node == null) { return ""; } return node.InnerText; } catch (Exception e) { throw e; } } } /// <summary> /// 获取指定item中的属性 /// </summary> /// <param name="index">item索引</param> /// <param name="item">item子元素的属性描述</param> /// <returns></returns> public string this[int index,RssItem item]{ get { try { XmlNode node = FindChildNode(items[index], item.ToString()); if (node == null) { return ""; } return node.InnerText; } catch (Exception e) { throw e; } } } /// <summary> /// 获取item节点的计数 /// </summary> public int TotalItemNum { get { return items.Count; } } /// <summary> /// 新建一个rss 2.0解析器 /// </summary> public RssReader() { doc = new XmlDocument(); items = new List<XmlNode>(); } /// <summary> /// 从文件或url中加载xml文档 /// </summary> /// <param name="filename">文件名或url地址</param> public void Load(string filename) { try { doc.Load(filename); XmlNode rssnode = FindChildNode(doc, "rss"); rootnode = FindChildNode(rssnode, "channel"); GetItemsDetail(); } catch (Exception e) { throw e; } } /// <summary> /// 从文本中加载xml文档 /// </summary> /// <param name="xmltext">xml文本</param> public void LoadXml(string xmltext) { try { doc.LoadXml(xmltext); XmlNode rssnode = FindChildNode(doc, "rss"); rootnode = FindChildNode(rssnode, "channel"); GetItemsDetail(); } catch (Exception e) { throw e; } } /// <summary> /// 释放所有资源 /// </summary> public void Close() { items = null; rootnode = null; doc = null; } /// <summary> /// 查找子节点 /// </summary> /// <param name="node">父节点</param> /// <param name="name">节点名称</param> /// <returns>子节点</returns> public XmlNode FindChildNode(XmlNode node, string name) { XmlNode childNode = null; for (int i = 0; i < node.ChildNodes.Count; i++) { if (node.ChildNodes[i].Name == name && node.ChildNodes[i].ChildNodes.Count > 0) { childNode = node.ChildNodes[i]; return childNode; } } return childNode; } /// <summary> /// 获取item /// </summary> private void GetItemsDetail() { foreach (XmlNode n in rootnode) { if (n.Name == "item") { items.Add(n); } } } ~RssReader() { try { } catch (Exception e) { } } public static void Main() { RssReader rr = new RssReader(); rr.Load("http://top.baidu.com/rss/top10.xml"); string ss = rr[RssChannel.title]; string time = rr[0,RssItem.pubDate]; } } /// <summary> /// RSS频道 /// </summary> public enum RssChannel { /// <summary> /// 标题 /// </summary> title, /// <summary> /// 连接 /// </summary> link, /// <summary> /// 描述 /// </summary> description, /// <summary> /// 语言,可选 /// </summary> language, /// <summary> /// 版权 /// </summary> copyright, /// <summary> /// 内容发布者的邮件联系方式 /// </summary> managingEditor, /// <summary> /// 网站管理员的邮件联系方式 /// </summary> webMaster, /// <summary> /// 发布时间 /// </summary> pubDate, /// <summary> /// 最后一次更新的时间 /// </summary> lastBuildDate, /// <summary> /// 类别 /// </summary> category, /// <summary> /// rss生成器的名称 /// </summary> generator, /// <summary> /// Allows processes to register with a cloud to be notified /// of updates to the channel, implementing a lightweight /// publish-subscribe protocol for RSS feeds. /// </summary> cloud, /// <summary> /// ttl stands for time to live. It's a number of minutes /// that indicates how long a channel can be cached before /// refreshing from the source. /// </summary> ttl, /// <summary> /// Specifies a GIF, JPEG or PNG image that can be displayed with the channel. /// </summary> image, /// <summary> /// The PICS rating for the channel. /// </summary> rating, /// <summary> /// Specifies a text input box that can be displayed with the channel. /// </summary> textInput, /// <summary> /// A hint for aggregators telling them which hours they can skip. /// </summary> skipHours, /// <summary> /// A hint for aggregators telling them which days they can skip. /// </summary> skipDays } /// <summary> /// RSS Item /// </summary> public enum RssItem { /// <summary> /// 标题 /// </summary> title, /// <summary> /// 连接 /// </summary> link, /// <summary> /// 描述 /// </summary> description, /// <summary> /// Email address of the author of the item. /// </summary> author, /// <summary> /// Includes the item in one or more categories. /// </summary> category, /// <summary> /// URL of a page for comments relating to the item. /// </summary> comments, /// <summary> /// Describes a media object that is attached to the item. /// </summary> enclosure, /// <summary> /// A string that uniquely identifies the item. /// </summary> guid, /// <summary> /// Indicates when the item was published. /// </summary> pubDate, /// <summary> /// The RSS channel that the item came from. /// </summary> source } /// <summary> /// RSS中的图片项 /// </summary> public enum RssImage { title, link, url } }