Designs | Resume | KamranAyub.com | email: ayubx003 (@umn.edu)

« Hell Hath Frozen Over: All Fake Musical Instruments Are Compatible [Amazingness] | Main | Fix: Suckerfish IE7 Disappear/Sticky Bug [Yay!] »

How To: Read RSS/Atom Feed Using .NET and the XmlDataSource With Or Without Namespaces [Coding] | Posted at 12:43 PM

Today I was working on my site and I wanted to show some recent blog entries on the side. Used to PHP I was looking for an RSS reader I could use. I found one but it didn’t quite work with what I needed.

Stupidly, I realized that all the things I needed were right there!

Hit the jump for all the details!

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

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 my company blog’s feed as an example, which is originally made by Blogger and is 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:XmlDataSource ID="xmlISBlog" DataFile="http://feeds.feedburner.com/IntrepidStudios" TransformFile="RemoveNamespaces.xsl" XPath="feed/entry" runat="server"></asp:XmlDataSource>

 

<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.

Filed Under: How To Tips and Tricks Web & Internet

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About the Author

Kamran
Divide by Zero is Kamran Ayub's personal blog. Kamran owns and operates Intrepid Studios, a web design and development firm based in Minneapolis. Despite the above picture, Kamran is usually an intelligent person. Please forgive him.

View Kamran Ayub's profile on LinkedIn

Add to Technorati Favorites