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

Comments

11/13/2009 10:05:45 AM #

Word to the wise.  Don't title your blog post with BlogEngine.Net.  A bunch of spammers will try to post to your blog comments trying to get links back.

kadesmith United States |

2/1/2010 7:37:19 PM #

Thanks for the solution - it seems reasonably straightforward.  Can you control the number of entries.  I see that you have 5 on the front page, is this the default for your RSS feed, which seems low for a feed but a good number for the number of recent posts on the front page.

soakaways United States |

2/2/2010 6:38:07 PM #

I controlled the number of posts in the admin panel.  I just set the feed to display 5 posts.  You could control the number of items in the code behind using an XPath Query.  If you want more info on visit stackoverflow.com/.../get-a-specific-number-of-results-from-an-xmldocument-xpath-query

kadesmith United States |

2/2/2010 6:10:29 PM #

When trying this I just get a blank page, pulling either your syndication or my own.

Any ideas why this would happen? I am not sure I am creating the pages properly.

Ian United States |

2/2/2010 6:28:05 PM #

Ian, do you have a file called XSLTFile.xslt in the same directory as the page you are trying to show the RSS on?

kadesmith United States |

2/2/2010 6:31:49 PM #

I do. did you need to build this app in Visual Studio, or can the pages just be created with notepad?

Ian United States |

2/2/2010 6:34:31 PM #

Here is the exact code I am using.

test.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/.../xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Xml runat="server" ID="xml1" TransformSource="XSLTFile.xslt" />
    </div>
    </form>
</body>
</html>

test.aspx.cs

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 = "dev.fusionconnex.com/blog/syndication.axd";

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

    }
}


XSLTfile.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    Hello
    <table>
      <xsl:for-each select="rss/channel/item">
        <tr>
          <td>Test TEST TEST
            <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>

Ian United States |

2/2/2010 6:50:24 PM #

I wasn't able to get it to work with your syndication, but I used mine and was able to get it to work with the following code.  

test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/.../xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Xml runat="server" ID="xml1" TransformSource="XSLTFile.xslt" />
    </div>
    </form>
</body>
</html>

test.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;


public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Show the latest blog post snippet
        string blogURL = "www.onecubicleover.com/blog/syndication.axd";

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

    }
}

I left the XSLTFile the same as you had it.

kadesmith United States |

2/2/2010 6:58:30 PM #

Got it. Thanks for your help!

Ian United States |

2/2/2010 6:33:26 PM #

You should be able to just create the pages with notepad. Send me your code and I'll see if I can see what's going on.

kadesmith United States |

Comments are closed