Consuming RSS

by Idaho Web Designer 29. July 2009 19:52

I have been trying to display my most recent posts on the home page of  I was trying to consume the RSS provided by BlogEngine.Net using ASP.NET and C#.  When I used the code:

DataSet ds = new DataSet();
string blogURL = "";
XmlTextReader rdr = new XmlTextReader(blogURL);
datagrid.DataSource = ds.Tables[2];

I get the error "A column named 'title' already belongs to this DataTable" when it reaches the ds.ReadXml(rdr) line.  The problem lies in the syndication.axd file.  The tag <title> is used under <channel> as well as under <item>.  A quick XSLT file and <asp:Xml/> control instead of a datagrid on the default.aspx page was the solution for me.  Here's what I used:

On the default.aspx page I placed:

<asp:Xml runat="server" ID="xml1" TransformSource="XSLTFile.xslt" /> 

On the default.aspx.cs page I used

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.Data;

public partial class _default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        //Show the latest blog post snippet
        string blogURL = "";

        XmlDocument rssRead = new XmlDocument();
        xml1.Document = rssRead;


And finally my XSLTFile.xslt file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"

  <xsl:template match="/">
      <xsl:for-each select="rss/channel/item">
            <xsl:element name="a">
              <xsl:attribute name="href">
                <xsl:value-of select="link"/>
              <xsl:value-of select="title"/>

Tags: , , ,