RSS全称是Really Simple Syndication,是一个XML格式的文档。在他的最顶层,是一个<RSS></RSS> element,这个元素只有一个<channel></channel>的子节点,在channel子节点内包含了所有的内容。
channel节点内必须包含的内容:
<title>channel的名称,使用户引用你的服务的ID,一般使你网站的名称。//比如:ScottGu's Blog
<link> 此RSS文件表示的网站的路径。//比如:http://weblogs.asp.net/scottgu/default.aspx
<description> 对此channel的描述。
channel节点内可选的内容:
<language> channel的语言。//fr (French), de (German), it (Italian), nl (Dutch), el (Greek), es (Spanish), pt (Portuguese), ar (Arabic), he (Hebrew), ru (Russian), zh (Chinese), ja (Japanese), hi (Hindi), ur (Urdu), and sa (Sanskrit).
<copyright> 版权声明。//Copyright 2002, Spartanburg Herald-Journal
<managingEditor> 负责编辑内容的人的电子邮件地址。
<webMaster> 负责网站技术方面的人的电子邮件地址。
<pubDate> 发布日期。
其他的还有:
skipDays,skipHours,textInput,rating,image,ttl,cloud,docs,generator,category,lastBuildDate
在<channel>中包含的最重要的东西是<item>,他就像报纸上的一篇报道,博客上的一篇文章。你可以拥有任意数量的<item>。他所有的元素都是可选的。但是title和description两个至少其中一个要有。
<title> item的标题。
<link> item所指向的内容的路径。
<description> item的摘要。
<author> 作者。
<category> item的分类。
其他的还有:comments,enclosure,guid,pubDate,source。
简单的例子。
首先获取rss文件
//Get rss file.2
private XmlDocument GetRSSData() 3
{4
XmlDocument doc = new XmlDocument();5
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.sohu.com/");6
request.AllowAutoRedirect = true;7
WebResponse response = request.GetResponse();8
Stream stream = response.GetResponseStream();9
Byte[] pageData = new byte[(int)stream.Length];10
stream.Read(pageData, 0, (int)stream.Length);11
string rssXml = Encoding.Default.GetString(pageData);12
doc.LoadXml(rssXml);13
return doc;14
}接着解析
XmlDocument doc = GetRSSData();2

3
//Get title4
XmlNode titleNode = doc.SelectSingleNode("/rss/channel/title");5
m_titleInformationLabel.Text = titleNode.InnerText;6

7
//Get description8
XmlNode descriptionNode = doc.SelectSingleNode("/rss/channel/description");9
m_descriptionInformationLabel.Text = descriptionNode.InnerText;10

11
//Get items12
XmlNodeList itemNodes = doc.SelectNodes("/rss/channel/item");13
foreach (XmlNode node in itemNodes)14
{15
XmlNode itemTitleNode = node.SelectSingleNode("./title");16
m_itemsListBox.Items.Add(itemTitleNode.InnerText);17
}

浙公网安备 33010602011771号