如何打造RSS阅读器
电子科技大学软件学院03级02班 周银辉
关键点:下载RSS订阅所对应的XML文件,解析该XML文件
该类XML文件其形如:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace DemoForRss.RSS
{
public class RssItem
{
private string title;
private string link;
private string description;
/**//// <summary>
/// 获取该项的标题
/// </summary>
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
}
}
/**//// <summary>
/// 获取该项的Url地址
/// </summary>
public string Link
{
get
{
return this.link;
}
}
/**//// <summary>
/// 获取该项的描述信息
/// </summary>
public string Description
{
get
{
return this.description;
}
}
public RssItem()
{
}
/**//// <summary>
/// 从“item”节点构造一个项
/// </summary>
/// <param name="itemNode"></param>
public RssItem(XmlNode itemNode)
{
if (itemNode != null)
{
XmlNode nodeTitle = itemNode.SelectSingleNode("title");
if (nodeTitle != null)
{
this.title = nodeTitle.InnerText;
}
XmlNode nodeLink = itemNode.SelectSingleNode("link");
if (nodeLink != null)
{
this.link = nodeLink.InnerText;
}
XmlNode nodeDescription = itemNode.SelectSingleNode("description");
if (nodeDescription != null)
{
this.description = nodeDescription.InnerText;
}
}
}
}
}
关键点:下载RSS订阅所对应的XML文件,解析该XML文件
该类XML文件其形如:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
<title>博客园-this.Study(DateTime.Now)</title>
<link>http://www.cnblogs.com/zhouyinhui/</link>
<description>专业因为专注</description>
<language>zh-cn</language>
<lastBuildDate>Mon, 16 Oct 2006 05:33:31 GMT</lastBuildDate>
<pubDate>Mon, 16 Oct 2006 05:33:31 GMT</pubDate>
<ttl>60</ttl>
<item>
<title>真正的代码宝库:Google Code Search</title>
<link>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html</link>
<dc:creator>周银辉</dc:creator>
<author>周银辉</author>
<pubDate>Fri, 13 Oct 2006 09:12:00 GMT</pubDate>
<guid>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html</guid>
<wfw:comment>http://www.cnblogs.com/zhouyinhui/comments/528421.html</wfw:comment>
<comments>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html#Feedback</comments>
<slash:comments>2</slash:comments>
<wfw:commentRss>http://www.cnblogs.com/zhouyinhui/comments/commentRss/528421.html</wfw:commentRss>
<trackback:ping>http://www.cnblogs.com/zhouyinhui/services/trackbacks/528421.html</trackback:ping>
<description>
<![CDATA[ Google推出代码搜索 ,可以搜索到无数的开源代码,有让人欣喜若狂的感觉啊.<BR><BR>不用多说,快去看看 <A href="http://www.google.com/codesearch">http://www.google.com/codesearch</A><img src ="http://www.cnblogs.com/zhouyinhui/aggbug/528421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://zhouyinhui.cnblogs.com/" target="_blank">周银辉</a> 2006-10-13 17:12 <a href="/zhouyinhui/archive/2006/10/13/528421.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>
]]>
</description>
</item>
</channel>
</rss>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
<title>博客园-this.Study(DateTime.Now)</title>
<link>http://www.cnblogs.com/zhouyinhui/</link>
<description>专业因为专注</description>
<language>zh-cn</language>
<lastBuildDate>Mon, 16 Oct 2006 05:33:31 GMT</lastBuildDate>
<pubDate>Mon, 16 Oct 2006 05:33:31 GMT</pubDate>
<ttl>60</ttl>
<item>
<title>真正的代码宝库:Google Code Search</title>
<link>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html</link>
<dc:creator>周银辉</dc:creator>
<author>周银辉</author>
<pubDate>Fri, 13 Oct 2006 09:12:00 GMT</pubDate>
<guid>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html</guid>
<wfw:comment>http://www.cnblogs.com/zhouyinhui/comments/528421.html</wfw:comment>
<comments>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html#Feedback</comments>
<slash:comments>2</slash:comments>
<wfw:commentRss>http://www.cnblogs.com/zhouyinhui/comments/commentRss/528421.html</wfw:commentRss>
<trackback:ping>http://www.cnblogs.com/zhouyinhui/services/trackbacks/528421.html</trackback:ping>
<description>
<![CDATA[ Google推出代码搜索 ,可以搜索到无数的开源代码,有让人欣喜若狂的感觉啊.<BR><BR>不用多说,快去看看 <A href="http://www.google.com/codesearch">http://www.google.com/codesearch</A><img src ="http://www.cnblogs.com/zhouyinhui/aggbug/528421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://zhouyinhui.cnblogs.com/" target="_blank">周银辉</a> 2006-10-13 17:12 <a href="/zhouyinhui/archive/2006/10/13/528421.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>
]]>
</description>
</item>
</channel>
</rss>
如何下载
可以简单地利用 System.Net.WebClient 类,其对象有一个OpenRead(string url)方法,该方法可以打开一个Stream,可以使用该Stream来创建一个XmlDocument
WebClient client = new WebClient();
XmlDocument doc = null;
try
{
using (Stream stream = client.OpenRead(url))
{
doc = new XmlDocument();
doc.Load(stream);
}
}
catch
{
throw;
}
if (doc != null)
{
// do something else
}
XmlDocument doc = null;
try
{
using (Stream stream = client.OpenRead(url))
{
doc = new XmlDocument();
doc.Load(stream);
}
}
catch
{
throw;
}
if (doc != null)
{
// do something else
}
如何解析下载的XML文件:
注意到这样一个层次关系:一个RSS包含n个RssFeed,一个RssFeed包含n个RssChannel,RssChannel一个包含n个Item。这里的Item即一个项,它通常是一篇文章,包含标题、Url地址、简短描述等等.
RssItem:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace DemoForRss.RSS
{
public class RssItem
{
private string title;
private string link;
private string description;
/**//// <summary>
/// 获取该项的标题
/// </summary>
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
}
}
/**//// <summary>
/// 获取该项的Url地址
/// </summary>
public string Link
{
get
{
return this.link;
}
}
/**//// <summary>
/// 获取该项的描述信息
/// </summary>
public string Description
{
get
{
return this.description;
}
}
public RssItem()
{
}
/**//// <summary>
/// 从“item”节点构造一个项
/// </summary>
/// <param name="itemNode"></param>
public RssItem(XmlNode itemNode)
{
if (itemNode != null)
{
XmlNode nodeTitle = itemNode.SelectSingleNode("title");
if (nodeTitle != null)
{
this.title = nodeTitle.InnerText;
}
XmlNode nodeLink = itemNode.SelectSingleNode("link");
if (nodeLink != null)
{
this.link = nodeLink.InnerText;
}
XmlNode nodeDescription = itemNode.SelectSingleNode("description");
if (nodeDescription != null)
{
this.description = nodeDescription.InnerText;
}
}
}
}
}
RssChannel
RssChannel
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5
6namespace DemoForRss.RSS
7{
8 /**//// <summary>
9 /// Rss频道,包含0个或多个RssItem
10 /// </summary>
11 public class RssChannel
12 {
13 private string title;
14 private string link;
15 private List<RssItem> itemList = new List<RssItem>();
16
17 /**//// <summary>
18 /// 获取该项的标题
19 /// </summary>
20 public string Title
21 {
22 get
23 {
24 return this.title;
25 }
26
27 }
28
29 /**//// <summary>
30 /// 获取该项的Url地址
31 /// </summary>
32 public string Link
33 {
34 get
35 {
36 return this.link;
37 }
38
39 }
40
41 /**//// <summary>
42 /// 获取其RssItem列表的只读副本
43 /// </summary>
44 public List<RssItem> ItemList
45
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5
6namespace DemoForRss.RSS
7{
8 /**//// <summary>
9 /// Rss频道,包含0个或多个RssItem
10 /// </summary>
11 public class RssChannel
12 {
13 private string title;
14 private string link;
15 private List<RssItem> itemList = new List<RssItem>();
16
17 /**//// <summary>
18 /// 获取该项的标题
19 /// </summary>
20 public string Title
21 {
22 get
23 {
24 return this.title;
25 }
26
27 }
28
29 /**//// <summary>
30 /// 获取该项的Url地址
31 /// </summary>
32 public string Link
33 {
34 get
35 {
36 return this.link;
37 }
38
39 }
40
41 /**//// <summary>
42 /// 获取其RssItem列表的只读副本
43 /// </summary>
44 public List<RssItem> ItemList
45