A bit about Atom
Displaying Atom feeds in a .NET page is pretty straight forward with just an XML control, and an XSL stylesheet for transforming the XML. As well as for Atom feeds, the info below can also be used for merging an RSS feed into a webpage.
Atom is an XML-based format used by many blogs and the like, allowing for the syndication of information across the web. More info can be found about Atom at:
Another bit but about XSL
Technically XSL is family of technologies that allow for the tranformation and presentation of XML. XSLT is the language used for transformations, and is a subset of XSL, but for most purposes people use XSL and XSLT interchangably. Have a look at W3C - The Extensible Stylesheet Language Family (XSL) for more info.
And Finally - Getting an Atom feed in to a page
To get started with displaying an Atom feed in a .NET page, first off just add an XML control into the web page.
<asp:Xml ID="xmlRSSFeed01" runat="server"></asp:xml>
Next, an XSL file is needed that will be used by the control to format the XML from the Atom feed into HTML.
The following code is just a basic layout to display an Atom feed and is placed into an XSL file within the project.
Three XSL templates are used. The first template is used to display a header and info about the feed. Within this template a second template is called which iterates through each entry in the feed and displays the date, title and summary. The third template is used to format the datetime stamp of the entry and return it in a more human friendly as opposed to the ISO 8601 UTC standard. Feel free to take this code and paste into an XSL file to start working with transforming an Atom feed.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/atom:feed">
<table>
<tr>
<h1>
<xsl:value-of select="atom:title"/>
</h1>
<p>
<a href="{atom:link[@rel='alternate']/@href}">
<xsl:value-of select="atom:link[@rel='alternate']/@href"/>
</a>
</p>
<p>
<xsl:value-of select="atom:subtitle"/>
</p>
</tr>
<tr>
<th>Date</th>
<th>Title</th>
</tr>
<xsl:apply-templates select="atom:entry" />
</table>
</xsl:template>
<xsl:template match="atom:entry">
<tr>
<td>
<p>
<xsl:call-template name="FormatDateTime">
<xsl:with-param name="DateTime" select="atom:published"/>
</xsl:call-template>
</p>
</td>
<td>
<p>
<a href="{atom:link[@rel='alternate']/@href}">
<xsl:value-of select="atom:title"/>
</a>
</p>
</td>
</tr>
<tr>
<td colspan="2">
<p>
<xsl:value-of select="atom:summary"/>
</p>
</td>
</tr>
</xsl:template>
<xsl:template name="FormatDateTime">
<xsl:param name="DateTime" />
<xsl:variable name="mo">
<xsl:value-of select="substring($DateTime,6,2)" />
</xsl:variable>
<xsl:variable name="day">
<xsl:value-of select="substring($DateTime,9,2)" />
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="substring($DateTime,1,4)" />
</xsl:variable>
<xsl:variable name="time">
<xsl:value-of select="substring($DateTime,12,8)" />
</xsl:variable>
<xsl:variable name="hh">
<xsl:value-of select="substring($time,1,2)" />
</xsl:variable>
<xsl:variable name="mm">
<xsl:value-of select="substring($time,4,2)" />
</xsl:variable>
<xsl:variable name="ss">
<xsl:value-of select="substring($time,7,2)" />
</xsl:variable>
<xsl:value-of select="$day" />
<xsl:text>/</xsl:text>
<xsl:value-of select="$mo" />
<xsl:text>/</xsl:text>
<xsl:value-of select="$year" />
</xsl:template>
</xsl:stylesheet>
To bring it all together requires just a few lines of code in the code behind file of the page.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set transform source for feed (the xsl file)
this.xmlRSSFeed01.TransformSource = "~/rss_feed.xsl";
//load Atom feed into XPathDocument and create navigator
XPathDocument xpDoc = new XPathDocument("http://linktoatomfeedhere/feed.xml");
XPathNavigator xpNav = xpDoc.CreateNavigator();
//load the XML into the XML control
this.xmlRSSFeed01.XPathNavigator = xpNav;
}
}
In conclusion
With the Atom feed, the .NET control, and the XSL file, the feed (and with a few tweaks, any RSS feed) can be integrated into a page in anyway required.
Displaying Atom feeds in a .NET page is pretty straight forward with just an XML control, and an XSL stylesheet for transforming the XML. As well as for Atom feeds, the info below can also be used for merging an RSS feed into a webpage.
Atom is an XML-based format used by many blogs and the like, allowing for the syndication of information across the web. More info can be found about Atom at:
Another bit but about XSL
Technically XSL is family of technologies that allow for the tranformation and presentation of XML. XSLT is the language used for transformations, and is a subset of XSL, but for most purposes people use XSL and XSLT interchangably. Have a look at W3C - The Extensible Stylesheet Language Family (XSL) for more info.
And Finally - Getting an Atom feed in to a page
To get started with displaying an Atom feed in a .NET page, first off just add an XML control into the web page.
<asp:Xml ID="xmlRSSFeed01" runat="server"></asp:xml>
Next, an XSL file is needed that will be used by the control to format the XML from the Atom feed into HTML.
The following code is just a basic layout to display an Atom feed and is placed into an XSL file within the project.
Three XSL templates are used. The first template is used to display a header and info about the feed. Within this template a second template is called which iterates through each entry in the feed and displays the date, title and summary. The third template is used to format the datetime stamp of the entry and return it in a more human friendly as opposed to the ISO 8601 UTC standard. Feel free to take this code and paste into an XSL file to start working with transforming an Atom feed.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/atom:feed">
<table>
<tr>
<h1>
<xsl:value-of select="atom:title"/>
</h1>
<p>
<a href="{atom:link[@rel='alternate']/@href}">
<xsl:value-of select="atom:link[@rel='alternate']/@href"/>
</a>
</p>
<p>
<xsl:value-of select="atom:subtitle"/>
</p>
</tr>
<tr>
<th>Date</th>
<th>Title</th>
</tr>
<xsl:apply-templates select="atom:entry" />
</table>
</xsl:template>
<xsl:template match="atom:entry">
<tr>
<td>
<p>
<xsl:call-template name="FormatDateTime">
<xsl:with-param name="DateTime" select="atom:published"/>
</xsl:call-template>
</p>
</td>
<td>
<p>
<a href="{atom:link[@rel='alternate']/@href}">
<xsl:value-of select="atom:title"/>
</a>
</p>
</td>
</tr>
<tr>
<td colspan="2">
<p>
<xsl:value-of select="atom:summary"/>
</p>
</td>
</tr>
</xsl:template>
<xsl:template name="FormatDateTime">
<xsl:param name="DateTime" />
<xsl:variable name="mo">
<xsl:value-of select="substring($DateTime,6,2)" />
</xsl:variable>
<xsl:variable name="day">
<xsl:value-of select="substring($DateTime,9,2)" />
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="substring($DateTime,1,4)" />
</xsl:variable>
<xsl:variable name="time">
<xsl:value-of select="substring($DateTime,12,8)" />
</xsl:variable>
<xsl:variable name="hh">
<xsl:value-of select="substring($time,1,2)" />
</xsl:variable>
<xsl:variable name="mm">
<xsl:value-of select="substring($time,4,2)" />
</xsl:variable>
<xsl:variable name="ss">
<xsl:value-of select="substring($time,7,2)" />
</xsl:variable>
<xsl:value-of select="$day" />
<xsl:text>/</xsl:text>
<xsl:value-of select="$mo" />
<xsl:text>/</xsl:text>
<xsl:value-of select="$year" />
</xsl:template>
</xsl:stylesheet>
To bring it all together requires just a few lines of code in the code behind file of the page.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set transform source for feed (the xsl file)
this.xmlRSSFeed01.TransformSource = "~/rss_feed.xsl";
//load Atom feed into XPathDocument and create navigator
XPathDocument xpDoc = new XPathDocument("http://linktoatomfeedhere/feed.xml");
XPathNavigator xpNav = xpDoc.CreateNavigator();
//load the XML into the XML control
this.xmlRSSFeed01.XPathNavigator = xpNav;
}
}
In conclusion
With the Atom feed, the .NET control, and the XSL file, the feed (and with a few tweaks, any RSS feed) can be integrated into a page in anyway required.
Comments
Post a Comment