如何打造RSS阅读器
电子科技大学软件学院03级02班 周银辉
关键点:下载RSS订阅所对应的XML文件,解析该XML文件
该类XML文件其形如:
如何下载
可以简单地利用 System.Net.WebClient 类,其对象有一个OpenRead(string url)方法,该方法可以打开一个Stream,可以使用该Stream来创建一个XmlDocument
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
---------------------------------------------------------------------
下载Demo: http://files.cnblogs.com/zhouyinhui/DemoForRss.rar
电子科技大学软件学院03级02班 周银辉
关键点:下载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 {
46 get
47 {
48 return this.itemList;
49 }
50
51 }
52
53 public RssChannel()
54 {
55 }
56
57 /**//// <summary>
58 /// 由“channel”节点构造RssChannel对象
59 /// </summary>
60 /// <param name="nodeChannel"></param>
61 public RssChannel(XmlNode nodeChannel)
62 {
63 if (nodeChannel != null)
64 {
65 XmlNode nodeTitle = nodeChannel.SelectSingleNode("title");
66 if (nodeTitle != null)
67 {
68 this.title = nodeTitle.InnerText;
69 }
70
71 XmlNode nodeLink = nodeChannel.SelectSingleNode("link");
72 if (nodeLink != null)
73 {
74 this.link = nodeLink.InnerText;
75 }
76
77 foreach (XmlNode nodeItem in nodeChannel.SelectNodes("item"))
78 {
79 this.itemList.Add(new RssItem(nodeItem));
80 }
81 }
82 }
83 }
84}
85
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 {
46 get
47 {
48 return this.itemList;
49 }
50
51 }
52
53 public RssChannel()
54 {
55 }
56
57 /**//// <summary>
58 /// 由“channel”节点构造RssChannel对象
59 /// </summary>
60 /// <param name="nodeChannel"></param>
61 public RssChannel(XmlNode nodeChannel)
62 {
63 if (nodeChannel != null)
64 {
65 XmlNode nodeTitle = nodeChannel.SelectSingleNode("title");
66 if (nodeTitle != null)
67 {
68 this.title = nodeTitle.InnerText;
69 }
70
71 XmlNode nodeLink = nodeChannel.SelectSingleNode("link");
72 if (nodeLink != null)
73 {
74 this.link = nodeLink.InnerText;
75 }
76
77 foreach (XmlNode nodeItem in nodeChannel.SelectNodes("item"))
78 {
79 this.itemList.Add(new RssItem(nodeItem));
80 }
81 }
82 }
83 }
84}
85
RssFeed
RssFeed
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
namespace DemoForRss.RSS
{
/**//// <summary>
/// RssFeed,它由0个或多个RssChannel组成
/// </summary>
[Serializable()]
public class RssFeed
{
private List<RssChannel> channelList = new List<RssChannel>();
private string url = "";
private string path = "";
public RssFeed()
{
}
/**//// <summary>
/// 获取该Feed对应的Xml文件的本机地址,它是网络上的Xml文件的副本
/// </summary>
public string Path
{
get
{
return this.path;
}
}
/**//// <summary>
/// 获取该Feed对应的Xml文件的网络地址
/// </summary>
public string Url
{
get
{
return this.url;
}
}
/**//// <summary>
/// 获取频道列表(一般只有一个频道)
/// </summary>
public List<RssChannel> ChannelList
{
get
{
return channelList;
}
}
/**//// <summary>
/// 获取频道列表中的第一个频道(主频道)
/// </summary>
public RssChannel FirstChannel
{
get
{
if (channelList.Count > 0)
{
return channelList[0];
}
return null;
}
}
/**//// <summary>
/// 从指定网址加载RssFeed信息
/// </summary>
/// <param name="url"></param>
/// <param name="saveTo">将下载到的文件保存到这里,如果不保存请填写null</param>
public void LoadFormUrl(string url, string saveTo)
{
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)
{
foreach (XmlNode nodeRss in doc.SelectNodes("rss"))
{
if (nodeRss != null)
{
foreach (XmlNode nodeChannel in nodeRss.SelectNodes("channel"))
{
this.channelList.Add(new RssChannel(nodeChannel));
}
}
}
if (!string.IsNullOrEmpty(saveTo))
{
try
{
doc.Save(saveTo);
}
catch
{
}
}
}
this.url = url;
}
/**//// <summary>
/// 从指定文件加载RssFeed信息
/// </summary>
/// <param name="path"></param>
public void LoadFormPath(string path)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
foreach (XmlNode nodeRss in doc.SelectNodes("rss"))
{
if (nodeRss != null)
{
foreach (XmlNode nodeChannel in nodeRss.SelectNodes("channel"))
{
this.channelList.Add(new RssChannel(nodeChannel));
}
}
}
this.path = path;
}
catch
{
throw;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
namespace DemoForRss.RSS
{
/**//// <summary>
/// RssFeed,它由0个或多个RssChannel组成
/// </summary>
[Serializable()]
public class RssFeed
{
private List<RssChannel> channelList = new List<RssChannel>();
private string url = "";
private string path = "";
public RssFeed()
{
}
/**//// <summary>
/// 获取该Feed对应的Xml文件的本机地址,它是网络上的Xml文件的副本
/// </summary>
public string Path
{
get
{
return this.path;
}
}
/**//// <summary>
/// 获取该Feed对应的Xml文件的网络地址
/// </summary>
public string Url
{
get
{
return this.url;
}
}
/**//// <summary>
/// 获取频道列表(一般只有一个频道)
/// </summary>
public List<RssChannel> ChannelList
{
get
{
return channelList;
}
}
/**//// <summary>
/// 获取频道列表中的第一个频道(主频道)
/// </summary>
public RssChannel FirstChannel
{
get
{
if (channelList.Count > 0)
{
return channelList[0];
}
return null;
}
}
/**//// <summary>
/// 从指定网址加载RssFeed信息
/// </summary>
/// <param name="url"></param>
/// <param name="saveTo">将下载到的文件保存到这里,如果不保存请填写null</param>
public void LoadFormUrl(string url, string saveTo)
{
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)
{
foreach (XmlNode nodeRss in doc.SelectNodes("rss"))
{
if (nodeRss != null)
{
foreach (XmlNode nodeChannel in nodeRss.SelectNodes("channel"))
{
this.channelList.Add(new RssChannel(nodeChannel));
}
}
}
if (!string.IsNullOrEmpty(saveTo))
{
try
{
doc.Save(saveTo);
}
catch
{
}
}
}
this.url = url;
}
/**//// <summary>
/// 从指定文件加载RssFeed信息
/// </summary>
/// <param name="path"></param>
public void LoadFormPath(string path)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
foreach (XmlNode nodeRss in doc.SelectNodes("rss"))
{
if (nodeRss != null)
{
foreach (XmlNode nodeChannel in nodeRss.SelectNodes("channel"))
{
this.channelList.Add(new RssChannel(nodeChannel));
}
}
}
this.path = path;
}
catch
{
throw;
}
}
}
}
---------------------------------------------------------------------
下载Demo: http://files.cnblogs.com/zhouyinhui/DemoForRss.rar