Consuming BlogEngine.net 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 onecubicleover.com.  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 = "http://www.onecubicleover.com/blog/syndication.axd";
XmlTextReader rdr = new XmlTextReader(blogURL);
ds.ReadXml(rdr);
datagrid.DataSource = ds.Tables[2];
datagrid.DataBind();

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 = "http://www.onecubicleover.com/blog/syndication.axd";

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

    }
}

And finally my XSLTFile.xslt file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

Tags: , , ,

ASP.NET | CSS | RSS