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="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>
  <author>
    <name>Rajendran SP</name>
    <email>rajsp@example.com</email>
  </author>
  <entry>
    <title>Atom-Powered Robots Run Amok</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 text.</summary>
  </entry>
</feed>

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"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <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="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

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">
            <ItemTemplate>
            <br />
            <a target="_blank" href='<%# XPath("link[@rel='alternate']/@href") %>'>
                    <strong> <%# XPath("title").ToString()%> </strong></a>
            <br />
            </ItemTemplate>
        </asp:Repeater>

Step 8: Compile and run

Output:


























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">
            <ItemTemplate>
            <br />
            <a target="_blank" href='<%# XPath("link[@rel='alternate']/@href") %>'>
                    <strong> <%# XPath("title").ToString()%> </strong></a>
            <br />
       <%# XPath("content").ToString() %>
            </ItemTemplate>
        </asp:Repeater>

Note: No Code behind involved.

References:

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

    ReplyDelete