Thursday, May 3, 2012

How to feed Atom into your ASP.Net application using Repeater control?

In this blog post, I will show you step by step illustration on how to feed Atom into the ASP.Net application using Repeater control.

What is Repeater Control?
The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items.

What is Atom?
        Atom Syndication format was developed as an alternative to RSS used for web feeds. Atom syndication format was published as an IETF  proposed standard. For more information refer: Atom_Wiki

An example document in the Atom Syndication format
<feed xmlns="">
  <title>Example Feed</title>
  <subtitle>A subtitle.</subtitle>
  <link href="" rel="self" />
  <link href="" />
    <name>Rajendran SP</name>
    <title>Atom-Powered Robots Run Amok</title>
    <link href="" />
    <link rel="alternate" type="text/html" href=""/>
    <link rel="edit" href=""/>
    <summary>Some text.</summary>

The best way to learn more about Atom is go through the xml contents of the feed.

How to feed Atom - step by step:

Step 1: Create an empty website and add a Default.aspx web page.

Step 2: Create RemoveNamspaces.xsl document.
        Atom feed contains namespaces that are not supported in XMLDataSource. Hence we need to remove those namespaces while providing the feed as data source. For that create an xsl transform document using below contents and save it as RemoveNamespaces.xsl document. We will be using this document while feeding Atom as XML data source for Repeater control.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="*">
    <!-- Remove any prefixes -->
    <xsl:element name="{local-name()}">
      <!-- Work through attributes -->
      <xsl:for-each select="@*">
        <!-- Remove any attribute prefixes -->
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="."/>

Step 3: Add data repeater control.

Step 4: Select new Data source in smart tag.

Step 5: Add xml data source.

Step 6: Configure XML data source and provide details as in below screenshot.

Here for Atom feed we are going to provide following details.

Data file: provide the Atom blog post feed link.
Transform file: provide or browse the RemoveNamespaces.xsl document.
XPath: “feed/entry”

Step 7: In Source view of the aspx page, Add below item template code.
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
            <br />
            <a target="_blank" href='<%# XPath("link[@rel='alternate']/@href") %>'>
                    <strong> <%# XPath("title").ToString()%> </strong></a>
            <br />

Step 8: Compile and run


On an additional Note, you can also view the content/summary of the blog along with the title by using below code in source.
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
            <br />
            <a target="_blank" href='<%# XPath("link[@rel='alternate']/@href") %>'>
                    <strong> <%# XPath("title").ToString()%> </strong></a>
            <br />
       <%# XPath("content").ToString() %>

Note: No Code behind involved.


1 comment:

  1. Wow, It is very useful information about ASP.Net Application of feeding Atom into ASP.Net application using Repeater control. it is very useful blog with coding to all..

    ASP.Net Development India