如何打造RSS阅读器

如何打造RSS阅读器

                             电子科技大学软件学院03级02班 周银辉

关键点:下载RSS订阅所对应的XML文件,解析该XML文件

该类XML文件其形如:
None.gif<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
None.gif 
<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/">
None.gif 
<channel>
None.gif  
<title>博客园-this.Study(DateTime.Now)</title> 
None.gif  
<link>http://www.cnblogs.com/zhouyinhui/</link> 
None.gif  
<description>专业因为专注</description> 
None.gif  
<language>zh-cn</language> 
None.gif  
<lastBuildDate>Mon, 16 Oct 2006 05:33:31 GMT</lastBuildDate> 
None.gif  
<pubDate>Mon, 16 Oct 2006 05:33:31 GMT</pubDate> 
None.gif  
<ttl>60</ttl> 
None.gif 
<item>
None.gif  
<title>真正的代码宝库:Google Code Search</title> 
None.gif  
<link>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html</link> 
None.gif  
<dc:creator>周银辉</dc:creator> 
None.gif  
<author>周银辉</author> 
None.gif  
<pubDate>Fri, 13 Oct 2006 09:12:00 GMT</pubDate> 
None.gif  
<guid>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html</guid> 
None.gif  
<wfw:comment>http://www.cnblogs.com/zhouyinhui/comments/528421.html</wfw:comment> 
None.gif  
<comments>http://www.cnblogs.com/zhouyinhui/archive/2006/10/13/528421.html#Feedback</comments> 
None.gif  
<slash:comments>2</slash:comments> 
None.gif  
<wfw:commentRss>http://www.cnblogs.com/zhouyinhui/comments/commentRss/528421.html</wfw:commentRss> 
None.gif  
<trackback:ping>http://www.cnblogs.com/zhouyinhui/services/trackbacks/528421.html</trackback:ping> 
None.gif 
<description>
None.gif  
<![CDATA[ Google推出代码搜索&nbsp;,可以搜索到无数的开源代码,有让人欣喜若狂的感觉啊.<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>
None.gif  
]]> 
None.gif  
</description>
None.gif 
</item>
None.gif 
</channel>
None.gif 
</rss>


如何下载

可以简单地利用 System.Net.WebClient 类,其对象有一个OpenRead(string url)方法,该方法可以打开一个Stream,可以使用该Stream来创建一个XmlDocument
None.gif WebClient client = new WebClient();
None.gif XmlDocument doc 
= null;
None.gif
None.gif            
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                
using (Stream stream = client.OpenRead(url))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    doc 
= new XmlDocument();
InBlock.gif                    doc.Load(stream);
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

None.gif            
catch
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedBlockEnd.gif            }

None.gif
None.gif            
if (doc != null)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif               
// do something else
ExpandedBlockEnd.gif
            }


如何解析下载的XML文件:

注意到这样一个层次关系:一个RSS包含n个RssFeed,一个RssFeed包含n个RssChannel,RssChannel一个包含n个Item。这里的Item即一个项,它通常是一篇文章,包含标题、Url地址、简短描述等等.

RssItem:

ContractedBlock.gifExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Xml;
None.gif
None.gif
namespace DemoForRss.RSS
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
public class RssItem
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
private string title;
InBlock.gif        
private string link;
InBlock.gif        
private string description;
InBlock.gif
InBlock.gif  
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取该项的标题
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Title
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.title;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.title = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取该项的Url地址
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Link
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.link;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取该项的描述信息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Description
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.description;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public RssItem()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从“item”节点构造一个项
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="itemNode"></param>

InBlock.gif        public RssItem(XmlNode itemNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (itemNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlNode nodeTitle 
= itemNode.SelectSingleNode("title");
InBlock.gif                
if (nodeTitle != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.title = nodeTitle.InnerText;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                XmlNode nodeLink 
= itemNode.SelectSingleNode("link");
InBlock.gif                
if (nodeLink != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.link = nodeLink.InnerText;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                XmlNode nodeDescription 
= itemNode.SelectSingleNode("description");
InBlock.gif                
if (nodeDescription != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.description = nodeDescription.InnerText;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

RssChannel
ContractedBlock.gifExpandedBlockStart.gifRssChannel
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gifusing System.Xml;
 5None.gif
 6None.gifnamespace DemoForRss.RSS
 7ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// Rss频道,包含0个或多个RssItem
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public class RssChannel
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        private string title;
14InBlock.gif        private string link;
15InBlock.gif        private List<RssItem> itemList = new List<RssItem>();
16InBlock.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
18InBlock.gif        /// 获取该项的标题
19ExpandedSubBlockEnd.gif        /// </summary>

20InBlock.gif        public string Title
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            get
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                return this.title;
25ExpandedSubBlockEnd.gif            }

26InBlock.gif           
27ExpandedSubBlockEnd.gif        }

28InBlock.gif
29ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
30InBlock.gif        /// 获取该项的Url地址
31ExpandedSubBlockEnd.gif        /// </summary>

32InBlock.gif        public string Link
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            get
35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
36InBlock.gif                return this.link;
37ExpandedSubBlockEnd.gif            }

38InBlock.gif           
39ExpandedSubBlockEnd.gif        }

40InBlock.gif
41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
42InBlock.gif        /// 获取其RssItem列表的只读副本
43ExpandedSubBlockEnd.gif        /// </summary>

44InBlock.gif        public List<RssItem> ItemList
45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
46InBlock.gif            get
47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
48InBlock.gif                return this.itemList;
49ExpandedSubBlockEnd.gif            }

50InBlock.gif            
51ExpandedSubBlockEnd.gif        }

52InBlock.gif
53InBlock.gif        public RssChannel()
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55ExpandedSubBlockEnd.gif        }

56InBlock.gif
57ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
58InBlock.gif        /// 由“channel”节点构造RssChannel对象
59InBlock.gif        /// </summary>
60ExpandedSubBlockEnd.gif        /// <param name="nodeChannel"></param>

61InBlock.gif        public RssChannel(XmlNode nodeChannel)
62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
63InBlock.gif            if (nodeChannel != null)
64ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
65InBlock.gif                XmlNode nodeTitle = nodeChannel.SelectSingleNode("title");
66InBlock.gif                if (nodeTitle != null)
67ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
68InBlock.gif                    this.title = nodeTitle.InnerText;
69ExpandedSubBlockEnd.gif                }

70InBlock.gif
71InBlock.gif                XmlNode nodeLink = nodeChannel.SelectSingleNode("link");
72InBlock.gif                if (nodeLink != null)
73ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
74InBlock.gif                    this.link = nodeLink.InnerText;
75ExpandedSubBlockEnd.gif                }

76InBlock.gif
77InBlock.gif                foreach (XmlNode nodeItem in nodeChannel.SelectNodes("item"))
78ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
79InBlock.gif                    this.itemList.Add(new RssItem(nodeItem));
80ExpandedSubBlockEnd.gif                }

81ExpandedSubBlockEnd.gif            }

82ExpandedSubBlockEnd.gif        }

83ExpandedSubBlockEnd.gif    }

84ExpandedBlockEnd.gif}

85None.gif

 

RssFeed

ContractedBlock.gifExpandedBlockStart.gifRssFeed
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Net;
None.gif
using System.Xml;
None.gif
using System.IO;
None.gif
None.gif
None.gif
namespace DemoForRss.RSS
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// RssFeed,它由0个或多个RssChannel组成
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [Serializable()]
InBlock.gif    
public class RssFeed
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private List<RssChannel> channelList = new List<RssChannel>();
InBlock.gif        
private string url = "";
InBlock.gif        
private string path = "";
InBlock.gif
InBlock.gif        
public RssFeed()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取该Feed对应的Xml文件的本机地址,它是网络上的Xml文件的副本
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Path
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.path;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取该Feed对应的Xml文件的网络地址
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Url
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.url;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取频道列表(一般只有一个频道)
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public List<RssChannel> ChannelList
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return channelList;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取频道列表中的第一个频道(主频道)
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public RssChannel FirstChannel
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (channelList.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return channelList[0];
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从指定网址加载RssFeed信息
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="url"></param>
ExpandedSubBlockEnd.gif        
/// <param name="saveTo">将下载到的文件保存到这里,如果不保存请填写null</param>

InBlock.gif        public void LoadFormUrl(string url, string saveTo)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WebClient client 
= new WebClient();
InBlock.gif            XmlDocument doc 
= null;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (Stream stream = client.OpenRead(url))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    doc 
= new XmlDocument();
InBlock.gif                    doc.Load(stream);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (doc != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (XmlNode nodeRss in doc.SelectNodes("rss"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (nodeRss != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
foreach (XmlNode nodeChannel in nodeRss.SelectNodes("channel"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
this.channelList.Add(new RssChannel(nodeChannel));
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if (!string.IsNullOrEmpty(saveTo))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        doc.Save(saveTo);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
this.url = url;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从指定文件加载RssFeed信息
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="path"></param>

InBlock.gif        public void LoadFormPath(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlDocument doc 
= new XmlDocument();
InBlock.gif                doc.Load(path);
InBlock.gif
InBlock.gif                
foreach (XmlNode nodeRss in doc.SelectNodes("rss"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (nodeRss != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
foreach (XmlNode nodeChannel in nodeRss.SelectNodes("channel"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
this.channelList.Add(new RssChannel(nodeChannel));
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
this.path = path;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


---------------------------------------------------------------------

下载Demohttp://files.cnblogs.com/zhouyinhui/DemoForRss.rar
展开阅读全文

没有更多推荐了,返回首页