65.9K
CodeProject 正在变化。 阅读更多。
Home

博客、RSS 新闻源和 ATOM [第二部分]

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.70/5 (13投票s)

2005年2月12日

7分钟阅读

viewsIcon

147370

downloadIcon

648

本文向您展示 RSS 1.0、RSS 2.0 和 ATOM 标准的结构,以及如何轻松制作 RSS 源和使用其他人的源。

引言

在本系列的第一部分中,我向您展示了 RSS 版本的历史以及新闻源的新标准 Atom。我们还介绍了新闻阅读器应用程序的功能。我想现在您一定已经感受到了这些博客的重要性。我希望您已经在使用所介绍的各种引擎中的一个安装了自己的博客,您可以在Cairo Cafe查看我的博客。在这一部分中,我们将分析 RSS 版本的格式并快速了解 Atom 格式,我们将制作一个自定义 RSS 源,为简单起见,我们将使用我们开发的同一源。请注意,我们将坚持使用 RSS 2.0,因为它是最知名的 RSS 版本,也是最简单的版本。

背景

RSS 1.0、RSS 2.0 和 Atom 都是基于 XML 的语言,它们遵循一个模式,每个输出都根据源本身的模式引入,RSS 2.0 是最简单的一种,并且比其他格式更广泛地使用。我们在这里将坚持使用 RSS 2.0,因为它是最简单的。在本文结束时,您将能够制作自己的新闻源,并且能够使用其他网站的源,例如 Code Project 为 ASP.NET 类别提供的最新文章。

不同的源格式和 OPML

RSS 1.0 格式

<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="htpp://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel rdf:about="http://www.KareemShaker.com/myBlog/Articles.xml">
<title>Kareem Shaker Website</title>
<link>http://www.KareemShaker.com/Articles.aspx</link>
<description>Kareem Shaker is an Egyptian developer who likes to
exchange knowledge with developers all over the worlds</description>
<image rdf:resource="http://www.KareemShaker.com/Articles/images/Logo.gif"/>
<items>
<rdf:Seq>
<rdf:li resource="http://www.KareemShaker.com/Articles.aspx?Id=3212" />
<rdf:li resource="http://www.KareemShaker.com/Articles.aspx?Id=3552" />
</rdf:Seq>
</items>
</channel>
<!--Declaration for all the items that are used 
above at the channel elements-->
<image rdf:about="http://www.KareemShaker.com/images/Logo.gif">
<title>KareemShaker.com</title>
<link>http://www.KareemShaker.com</link>
<url>http://www.KareemShaker.com/Articles/images/Logo.gif</url>
</image>
<item rdf:about="http://www.KareemShaker.com/Articles.aspx?Id=3212">
<title>DataSet Nitty Gritty</title>
<link>http://www.KareemShaker.com/Articles.aspx?Id=3212</link>
<description>Explains all disconnected environment provided by ADO.NET,
you will be using SQL Server and Oracle to build a simple POS System that
posts all the sales to a central headquarter</description>
<dc:date>2004-01-13T17:16:44.5605908-08:00</dc:date>
</item>
<item rdf:about="http://www.KareemShaker.com/Articles.aspx?Id=3552">
<title>Custom Controls Revisited</title>
<link>http://www.KareemShaker.com/Articles.aspx?Id=3552</link>
<description>Build a custom control that encapsulates the functionality
of Image gallery</description>
<dc:date>2004-01-13T17:16:44.5605908-08:00</dc:date>
</item>
</rdf:RDF>

如您所见,RSS 1.0 基于 RDF 并且其命名空间限定,您不需要了解更多关于 RDF 的信息,但如果您想深入了解它,您可以查看 W3C RDF 标准,所有引用的项目都在“channel”的结束元素之后列出,并且它在列在 channel 开放和结束标签之间的 items 集合中引用。这提供了在 RSS 1.0 文档中任何地方引用任何项目的灵活性。

RSS 2.0 格式

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>Kareem Shaker Website</title>
<link>http://www.KareemShaker.com/Articles.aspx</link>
<description>Kareem Shaker is an Egyptian developer who 
likes to exchange knowledge with developers all
over the world</description>
<image>
<url>"http://www.KareemShaker.com/Articles/images/Logo.gif"</url>
<title>KareemShaker.com</title>
<link>http://www.KareemShaker.com</link>
</image>
<item>
<title>DataSet Nitty Gritty</title>
<link>http://www.KareemShaker.com/Articles.aspx?Id=3212</link>
<description>Explains all disconnected environment provided by ADO.NET,
you will be using SQL Server and Oracle to build a simple POS System
that posts all the sales to a central headquarter</description>
<pubDate>Wed, 14 Jan 2004 16:16:16 GMT</pubDate>
</item>
<item>
<title>Custom Controls Revisited</title>
<link>http://www.KareemShaker.com/Articles.aspx?Id=3552</link>
<description>Build a custom control that encapsulates the functionality
of Image gallery</description>
<pubDate>Wed, 14 Jan 2004 20:50:44 GMT</pubDate>
</item>
</channel>
</rss>

RSS 2.0 是最简单的标准,并且被广泛使用。根元素是 RSS,版本属性是强制性的。如您所见,项目只是在 channel 主体中序列化,没有使用命名空间,RSS 2.0 易于使用和生成。RSS 2.0 文档需要一些元素,而其他元素是可选的。您可以在此处查看完整的详细模式定义。

Atom 0.3 格式

<?xml version="1.0" encoding="utf-8" ?>
<feed version="0.3" xml:lang="en-us" xmlns="http://purl.org/atom/ns#">
<title>Kareem Shaker Atom Feeder</title>
<link>http://www.KareemShaker.com/Articles.aspx</link>
<modified>2004-01-13T17:16:45.0004199-07:00</modified>
<tagline>Kareem Shaker is an Egyptian developer who likes to exchange
knowledge with developers all over the world</tagline>
<author>
<name>Kareem Shaker</name>
</author>
<entry>
<title>DataSet Nitty Gritty</title>
<link>http://www.KareemShaker.com/Articles.aspx?Id=3212</link>
<created>Wed, 14 Jan 2004 16:16:16 GMT</created>
<content type="text/html" mode="xml">
<body xmlns="http://www.w3.org/1999/xhtml">
<p>Explains all disconnected environment provided by
ADO.NET,you will be using SQL Server and Oracle to build a simple
POS System that posts all the sales to a central headquarter</p>
</body>
</content>
</entry>
<entry>
<title>Custom Controls Revisited</title>
<link>http://www.KareemShaker.com/Articles.aspx?Id=3552</link>
<created>Wed, 14 Jan 2004 16:02:16 GMT</created>
<content type="text/html" mode="xml">
<body xmlns="http://www.w3.org/1999/xhtml">
<p>Build a custom control that encapsulates the functionality
of Image gallery</p>
</body>
</content>
</entry>
</feed>

Atom 根元素是“feed”,版本属性是强制性的。实际上,Atom 标准介于 RSS 1.0 和 RSS 2.0 之间。它是命名空间限定的,但它不基于 RDF。在这里,您有一个“entry”元素而不是“item”。欲了解更多信息,您可以访问Atom 官方网站

OPML 格式

开放标记语言 (OPML) 不过是一个 XML 文件。它非常容易理解。主元素是“outline”,您只需提供 typetitledescriptionxmlUrlhtmlUrl 属性。您会发现所有新闻阅读器都支持读取 OPML 文件。我发现它非常有用,尤其是当我从朋友那里获取一些特色源时,他只需将他的频道导出为 OPML 文件并将其传递给我。然后我可以轻松导入该 OPML 文件。所有新闻阅读器都支持 OPML 导入/导出。

<?xml version="1.0" encoding="utf-8" ?>
<opml>
<head>
<title>Kareem Shaker's HotList</title>
</head>
<body>
<outline type="rss" title="Arabic Developers Bloggers"
description="This is a great collection of Arabic programming loggers"
xmlUrl="http://www.arabicbloggers.org/GiantFeed.rss"
htmlUrl="http://www.ArabicBloggers.org" />
<outline type="rss"
title="Kareem Shaker ASP.NET Blog"
description="Kareem Shaker's ASP.NET Community Central for Arabs"
xmlUrl="http://www.KareemShaker.com/blog.xml" 
htmlUrl="http://www.KareemShaker.com" />
<outline type="rss"
title="MacroCell Developers Blogs"
description="MacroCell is a innovative software house"
xmlUrl="http://www.MacroCell.com/team/blogs.rss"
htmlUrl="http://www.MacroCell.com/eg/team" />
</body>
</opml>

使用 Repeater 控件生成 RSS 2.0 文档

RSS 2.0 文档不过是一个遵循一个模式的 XML 文档,我们可以使用 System.XML 命名空间中的任何类来生成 XML 文档,我们可以使用 XMLTextWriter,或者 DataSet.WriteXML 方法,甚至可以使用 System.IO 类,但最简单的方法是使用 XMLTextWriter。如果您不熟悉这些类,您可以查看 C# Corner 上的文章。如您在上面的 RSS 2.0 文档中看到的,我们生成了一个标准输出,因此我们可以使用 Repeater 控件轻松输出我们想要的 RSS 文档。事实上,在查看了许多输出 RSS 文档的文章和方法后,我发现这是最简单的一种。为简单起见,我们将动态生成 RSS 项目,但在实际世界中,您将从 SQL Server 数据库中获取 RSS 项目,或者您可以指向定期生成的 RSS 文件。

页面 HTML

如您所见,我们只是编写或硬编码了将输出到 RSS 文档的行和项目,并且我们只是绑定了我们想要的项目值。不要忘记将页面指令的 contentType 属性分配为“text/xml”。

页面代码隐藏

我们将动态生成 RSS 项目,并在 web.config 中保存一个变量。此变量名为 rssItemsNumber。在生成 RSS 项目之前,我检查此变量的值,这只是要生成的项目计数。您应该在 configuration 节点之后和 system.web 节点之前添加此变量。

<appSettings>
    <add key="rssItemsNumber" value="10"></add>
</appSettings>

您应该在页面加载时读取此值

System.Int32 numberOfGeneratedItems =
System.Int32.Parse(
   System.Configuration.ConfigurationSettings.AppSettings["rssItemsNumber"]);
rssProducts.DataSource = GenerateRss(numberOfGeneratedItems);
rssProducts.DataBind();

GenerateRSS 函数负责生成一个 DataTable,我们将其绑定到 Repeater 控件。然后我们调用 repeater 的 Databind 方法将数据绑定到 Repeater。在 GenerateRSS 中,我们只需通过向 DataTable 对象添加所需的列来动态构建一个 DataTable 对象。然后我们通过循环该表并生成 RSS 项目来填充数据。RSS 项目的数量从 web.config 中获取,如上所示

private DataTable GenerateRss(int numberOfItems)
{   
    // create new table and call it rssItems
    DataTable dtItems = new DataTable("rssItems");
    // create all required data columns
    // id column
    DataColumn dcItem = new DataColumn();
    dcItem.ColumnName = "Id";
    dcItem.DataType = System.Type.GetType("System.Int32");
    dcItem.AutoIncrement= true;
    // add column to the datatable           
    dtItems.Columns.Add(dcItem);
    //title column
    dcItem = new DataColumn();
    dcItem.ColumnName = "title";
    dcItem.DataType = System.Type.GetType("System.String");
    dtItems.Columns.Add(dcItem);           
    // description column
    dcItem = new DataColumn();
    dcItem.ColumnName = "description";
    dcItem.DataType = System.Type.GetType("System.String");
    dtItems.Columns.Add(dcItem);
    // pubDate
    dcItem = new DataColumn();
    dcItem.ColumnName = "pubDate";
    dcItem.DataType = System.Type.GetType("System.DateTime");
    dtItems.Columns.Add(dcItem);
    // link
    dcItem = new DataColumn();
    dcItem.ColumnName = "link";
    dcItem.DataType = System.Type.GetType("System.String");
    dtItems.Columns.Add(dcItem);
    // make PK column
    DataColumn[]  pk = {dtItems.Columns[0]};
    dtItems.PrimaryKey = pk;
    // get new row to be added to the datatable
    DataRow drItem = dtItems.NewRow();
    // loop and generate the rss items up to the number 
    // of items mentioned in web.config
    for(int iCounter = 1; iCounter <= numberOfItems; iCounter++)
    {
        drItem["title"] = 
           "Product No. " + iCounter.ToString() + " From MacroCell";
        drItem["description"] = "Product " + iCounter.ToString() +
        " is the most promising product in our wide group";
        drItem["pubDate"] = DateTime.Now;
        drItem["link"]= "http://www.kareemshaker.com/products.aspx?id="
        + iCounter.ToString();
        // add to table
        dtItems.Rows.Add(drItem);
        // create new row
        drItem = dtItems.NewRow();
     }
     return dtItems;
}

代码很简单,注释也很详细,一旦您获得了返回并绑定到数据 RepeaterDataTable,您将获得作为 XML 文件的结果 RSS 2.0 文档。不要忘记我们已将 contentType 属性添加为“text/xml”。

如果您安装了新闻阅读器应用程序,您可以将此频道添加到其中,并且您可以看到新闻阅读器如何处理 RSS 文档。如果它正确读取并且没有抛出异常/错误,您可以发出格式良好的 RSS 文档,并且可以使用此 URL 将 RSS 频道添加到您的新阅读器:“https:///RssFeed/rss.aspx”,您可以将 localhost 替换为您的服务器。

使用 RSS 2.0 新闻源

您只需使用几行代码即可使用我们刚刚开发的 RSS 源。您可以使用相同的代码使用任何其他 RSS 源。这非常简单。您会找到一个名为 RSSReader 的 Web 项目。它只包含一个 WebForm,其中您会找到一个网格。此网格绑定到我们读取的 RSS 源。您会发现它非常容易理解。

在代码隐藏中,我们使用 DataSet 方法 ReadXml 读取 RSS 源。这是我见过的最简单的使用 RSS 源的方法,如果您深入了解 DataSet 如何在将分层数据 (XML 节点) 映射到表格数据 (DataTables) 时处理表,您会发现每个嵌套级别都会添加一个新的 DataTable,项目节点是 RSS 和 channel 节点之后的第二个嵌套节点,因此包含所有项目的表的索引是“2”。它是基于零的索引,所以如果我们将网格绑定到第二个表,它将包含所有 RSS 项目。如果您想读取 channel 的标题或描述,您应该读取表索引“1”。在页面加载事件处理程序中编写以下代码

private void Page_Load(object sender, System.EventArgs e)
{
    DataSet dsFeed = new DataSet("Feed");
    dsFeed.ReadXml("https:///RssFeed/rss.aspx");           
    recentProducts.DataSource = dsFeed.Tables[2];
    recentProducts.DataBind();
}

您可以为 ReadXml 方法提供任何其他 URL,您可以尝试 CodeProject 上关于 RSS 源的最新文章,您将获得所有最新文章列表。

结论

在这一部分中,我们了解了新闻源的各种格式。在这里,我们尝试深入研究 RSS 2.0,因为它是最简单和最广泛使用的一种。您还了解了如何制作自己的源以及如何使用他人的源。我想我将撰写第三部分来讨论更高级的主题。

© . All rights reserved.