在 ASP.NET 网页中消费 Atom Feed





0/5 (0投票)
在 ASP.NET 网页上消费/显示 Atom Feed
引言
继我之前的文章关于消费 RSS Feed 之后,我最近有一个需求是在网页上显示 Atom Feed 的内容。实际上,这是为了在我的网站上显示我自己的 Github 活动 Feed 的内容。
我不会在这篇文章中比较和对比 RSS 与 Atom。网上有很多资源已经做过了。
在网站上显示 Atom Feed 相对简单。从最简单的角度来说,Atom Feed 是一种 XML 结构,其中包含可以在网页上显示的数据。Atom Feed 被许多不同类型的网站用来推送信息。对这些信息感兴趣的用户可以订阅这些 Atom Feed 并消费它们。
Atom Feed 的结构
Atom Feed 具有以下基本结构。其他信息也包含在其中,但为了简洁起见,这些已被删除。
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<subtitle>A subtitle.</subtitle>
<link href="http://example.org/feed/" rel="self" />
<link href="http://example.org/" />
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
<updated>2003-12-13T18:30:02Z</updated>
<entry>
<title>My Atom feed</title>
<link href="http://example.org/2003/12/13/atom03" />
<link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
<link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some summary text.</summary>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>This is the entry content in HTML format.</p>
</div>
</content>
<author>
<name>Dominic Burford</name>
<email>example@mydomain.co.uk</email>
</author>
</entry>
</feed>
我感兴趣的信息,用于我的网站的是 Atom Feed 中的 Title
(标题)、Description
(描述)和 Publication Date
(发布日期)项目。
消费 Atom Feed
有几种不同的方法可以实现这一点,这只是一种方法。我使用 ASP.NET Web Form 实现了它,但一般原则可以应用于其他技术。
- 首先,我将一个
Gridview
(网格视图)控件添加到我的网页。Repeater
(重复器)控件也是完全可以接受的。<asp:GridView ID="gvAtom" runat="server"> <Columns> <asp:TemplateField> <ItemTemplate> <table width="100%" border="0" cellpadding="0" cellspacing="5"> <tr> <h3 style="color:#3E7CFF"><%#Eval("Title") %></h3> </tr> <tr> <%#Eval("PublishDate") %> </tr> <tr> <td colspan="2"> <hr /> <%#Eval("Description") %> </td> </tr> </table> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
- 在代码背后,我添加了一个新的方法来使用 Atom Feed 中的数据填充
Gridview
控件,如下所示。using System; using System.Collections.Generic; using System.Configuration; using System.Globalization; using System.Linq; using System.Xml.Linq; using System.ServiceModel.Syndication; using System.Xml; protected void Page_Load(object sender, EventArgs e) { this.PopulateAtomFeed(); } private void PopulateAtomFeed() { string atomFeedUrl = ConfigurationManager.AppSettings["AtomFeedUrl"]; List<Feeds> feeds = new List<Feeds>(); XmlTextReader reader = new XmlTextReader(atomFeedUrl); SyndicationFeed feed = SyndicationFeed.Load(reader); if (feed != null) { feeds.AddRange(feed.Items.Select(i => new Feeds { Title = i.Title.Text, PublishDate = i.PublishDate.DateTime.ToUniversalTime().ToString(CultureInfo.InvariantCulture), Description = ((TextSyndicationContent)(i.Content)).Text })); } gvAtom.DataSource = feeds; gvAtom.DataBind(); }
- 然后,我创建了一个简单的 POCO
Feed
(馈送)类,将 Atom Feed 项目映射到Gridview
控件。public class Feeds { public string Title { get; set; } public string PublishDate { get; set; } public string Description { get; set; } }
- 我将 Atom Feed 的 URL 存储在 web.config 中。这确保了如果需要可以轻松更改 URL。
<appSettings> <add key="AtomFeedUrl" value="http://www.somedomain.com/Atomfeed.Atom"/> </appSettings>
摘要
就这样了。正如我提供的链接所示,您可以在我的网站上自行查看 Atom Feed。
如果您可能要显示多个 Atom Feed,则可以轻松创建一个用户控件并将 Gridview
添加到该控件。为了使用户控件可重用,您可以添加一个公共属性到用户控件来设置 Atom Feed URL。然后将用户控件添加到您的网页并根据需要设置 Atom Feed URL 属性。
希望这篇文章能为您提供足够的信息,以便您在自己的应用程序中开始消费 Atom Feed。如果您希望我对本文中的任何内容进行进一步阐述,请随时留下评论。