Today I was working on my site and I wanted to show some recent blog entries on the side. If you are using SimplePie and PHP, you are in luck. Here's a way to accomplish a simple RSS reader using .NET.

Simple Feeds

For an XML document that doesn't use namespaces, here's how to do it (for the rest of you, skip to below):

  1. Create a new XmlDataSource
  2. Enter the URL to the RSS/Atom feed for DataFile
  3. Enter rss/channel/item for XPath (for Atom it might be different, it's just the path to the item tag).
  4. Create a repeater/data list/data grid or whatever you need and use that XmlDataSource.

Here's a quick example of what I used:

<asp:XmlDataSource ID="xmlDbZBlog" DataFile="http://feeds.feedburner.com/dividebyzeroblog?format=xml" XPath="rss/channel/item [position()<=5]" runat="server"></asp:XmlDataSource>

<asp:Repeater ID="rptBlog1" DataSourceId="xmlDbZBlog" runat="server">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate>

        <li><a href="<%# XPath("link") %>"><%# XPath("title") %></a></li>

    </ItemTemplate>
    <FooterTemplate>

        </ul>

        <a class="more" href="http://kamranayub.com/">Read more entries...</a>

    </FooterTemplate>

</asp:Repeater>

That's it! Note that I limited it to the latest 5 entries using the XPath [position() <= 5] syntax.

The Downside - Namespaces

For some reason, XmlDataSource does not support an XML document that uses namespaces. There are many other blog posts that talk about it, but let's just use a simple workaround to solve our dilemma. There are other workarounds that require more work but for our purposes all we want to do is show some recent posts.

I am using the old Intrepid Studios blog feed as an example, which was originally made by Blogger and was fed through FeedBurner. Our code above won't work with the XPath feed/entry because of the namespace issue.

Create a new XSL file, paste in the following:

<?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>
  1. Call it RemoveNamespaces.xsl
  2. In your code, for the TransformFile attribute for the XmlDataSource put the path to your XSL file.
  3. All done, your XPath should now work. You can use that XSL file for any other feed you have.

Here is the full code for my blog's feed. Notice the link attribute is a bit different than above. That's because in my feed the URL to the post is in the href attribute of the link tag with an attribute of rel="alternate".

<asp:Repeater ID="Repeater1" DataSourceId="xmlISBlog" runat="server">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate>
        <li><a href="<%# XPath("link[@rel='alternate']/@href") %>"><%# XPath("title") %></a></li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

I hope that helps someone out there! I had to go around searching before I found out how to do it… so I decided to compile it all here.