Community discussion forum

Reading an RSS file in ASP .NET

  • 3 months ago

    Hello,

    I'm trying to open two RSS files (http://forms.ncsl.org.uk/mediastore/image2/rss/ncsl-news.xml and http://www.dcsf.gov.uk/pns/feeds/latest.xml) and embed and format the content into an ASP .NET page (VB)

    I've searched and searched and nothing seems to work at all :(

    Can anyone help?

    Thanks,
    Aaron

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 3 months ago

    Hi,

    From my knowledge of RSS, you need to do two things.

    First, use a webrequest to get the newest RSS feed data from the two URLS.

    Then convert that into an XML document, there should be plenty of information on how to do this, from memory, you can do it by creating a new XMLDocument and setting the innerXML or something like that to the string you get back from the webrequest.

    After than, it is just a case of using repeaters with an XMLDataSource to generate your output.  You can style the repeaters as you like to provide the formatting that is easiest for your users.

    Does that help?

    Si

  • 3 months ago

    Thanks for the quick reply!

    Yeah it makes the process a little easy, but no idea on how to code it... have you got any examples or resources you can send or link me to?

    Thanks,

    Aaron

  • 3 months ago

    Hi,

    To get you started, i have done a very quick and dirty example of getting the text back from the RSS feed.

    Note, this doesnt have any exception handling on it, such as the datafeed going down etc, it is just how to get the information back from the RSS.

    Imports System.Xml

    Imports System.Net

    Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim objRequest As WebRequest = WebRequest.Create("http://forms.ncsl.org.uk/mediastore/image2/rss/ncsl-news.xml")

    Dim objResponse As WebResponse = Nothing

    Dim stmReader As IO.StreamReader = Nothing

    Dim strText As String = ""

    Dim xmlDoc As XmlDocument = New XmlDocument

    objResponse = objRequest.GetResponse

    stmReader =
    New IO.StreamReader(objResponse.GetResponseStream)

    strText = stmReader.ReadToEnd

    objResponse.Close()

    xmlDoc.InnerXml = strText

    End Sub

    End Class 

    If you need help with the repeater, then let me know.

    Regards

    Simon C

  • 3 months ago

     Again, thanks for your reply and yes can you help me with the repeater too :)

     Thank you!

  • 3 months ago

    Hi,

    The first link you had gave me problems using a repeater with that i dont have time to look at right now, but i knocked up a quick sample for the other.

    Here is the code behind

    Imports System.Xml

    Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim xmlDoc As XmlDocument = New XmlDocument

    xmlDoc.Load("http://www.dcsf.gov.uk/pns/feeds/latest.xml")Dim xdsData As XmlDataSource = New XmlDataSource

    xdsData.Data = xmlDoc.OuterXml

    xdsData.XPath = "/rss/channel/item"

    rptData.DataSource = xdsData

    rptData.DataBind()

    End Sub

    End Class 

    Here is the markup.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/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:Repeater runat="server" ID="rptData">

    <ItemTemplate>

    <p>

    <asp:Literal runat="server" Text='<%# XPAth("title") %>'></asp:Literal>

    <br />

    <asp:Literal runat="server" Text='<%#XPath("description") %>'></asp:Literal>

    </p>

    </ItemTemplate>

    </asp:Repeater>

    </div>

    </form>

    </body>

    </html>

    HTH

    Simon C

  • 3 months ago

    Simon you are a life saver!

     

    Thank you ever so much!

  • 3 months ago

     Sorry if this sounds stupid, but for some reason the following code doesnt work as I'd expect:

     <%
       Dim strRSS_Title As String
       Dim strRSS_FeedURL As String

       If Request.QueryString("rssfeed") = "" Then
          strRSS_Title = "Ormiston Education News"
          strRSS_FeedURL = "http://www.enterprisecapability.co.uk/ormiston/ormistonnews.asp"

       ElseIf Request.QueryString("rssfeed") = "dcsf" Then
          strRSS_Title = "DCSF News"
          strRSS_FeedURL = "http://forms.ncsl.org.uk/mediastore/image2/rss/ncsl-news.xml"

       ElseIf Request.QueryString("rssfeed") = "ncsl" Then
          strRSS_Title = "NCSL News"
          strRSS_FeedURL = "http://forms.ncsl.org.uk/mediastore/image2/rss/ncsl-news.xml"
       End If
    %>

       <p class="txtHeader2"><% = strRSS_Title %></p>
          <%
             Dim xmlNews_RSS As XmlDocument = New XmlDocument
             xmlNews_RSS.Load(strRSS_FeedURL)
             Dim xdsNews_RSS As XmlDataSource = New XmlDataSource
             xdsNews_RSS.Data = xmlNews_RSS.OuterXml
             xdsNews_RSS.XPath = "/rss/channel/item"
             rptNews_RSS.DataSource = xdsNews_RSS
             rptNews_RSS.DataBind()
          %>
          <asp:repeater id="rptNews_RSS" runat="server">
             <itemtemplate>
                <table class="tblGeneral" style="border-style:solid; border-width:1px; border-color:#DFDFDF;" width="95%" align="center">
                <tr>
                   <td class="cellBlank" width="100%" valign="top">
                      <p align="center"><span class="txtHeader3"><a href="<%# XPath("link") %>"><%# XPath("title") %></a></span><br/>
                      <span style="font-size:9px;"><%# XPath("pubDate") %></span></p>
                      <p><%# XPath("description") %></p>
                      <p><a href="mailto:your@friend.com?subject=<% = strRSS_Title %> Item&body=Hello!%0A%0AI just found this great news item on the Ormiston Education site: www.ormistoneducation.co.uk%0A%0ASee you soon!" style="font-size:9px;"><img src="images/icons/mail.jpg" border="0"/> Send to a Friend</a> | <a href="<% = strRSS_FeedURL %>" target="_blank"><img src="images/icons/rss.jpg" border="0"/> Subscribe to RSS</a></p>
                   </td>
                </tr>
                </table>
                <br/><br/>
          </itemtemplate>
       </asp:repeater>

     For starters I keep getting a

    Compiler Error Message: BC30451: Name 'strRSS_Title' is not declared.

    error, refering to:

    <p><a href="mailto:your@friend.com?subject=<% = strRSS_Title %> Item&body=Hello!%0A%0AI just found this great news item on the Ormiston Education site: www.ormistoneducation.co.uk%0A%0ASee you soon!" style="font-size:9px;"><img src="images/icons/mail.jpg" border="0"/> Send to a Friend</a> | <a href="<% = strRSS_FeedURL %>" target="_blank"><img src="images/icons/rss.jpg" border="0"/> Subscribe to RSS</a></p>

    Which, I've found, if I cut and paste the code above the <asp:repeater> tag it works fine...

    Also, for some reason when Request.QueryString("rssfeed") = "dcsf" it still loads the same result as Request.QueryString("rssfeed") = ""

    I think the issue is with the repeater - is there any other way around this?

    Thanks!

  • 3 months ago

    Hi,

    Try this for starters:

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title>Untitled Page</title>

    <script runat="server" language="vb">

    Dim strRSS_Title As String = ""

    Dim strRSS_FeedURL As String = ""

    </script>

    <%

    If Request.QueryString("rssfeed") = "" Then

    strRSS_Title = "Ormiston Education News"

    strRSS_FeedURL = "http://www.enterprisecapability.co.uk/ormiston/ormistonnews.asp"

    ElseIf Request.QueryString("rssfeed") = "dcsf" Then

    strRSS_Title = "DCSF News"

    strRSS_FeedURL = "http://forms.ncsl.org.uk/mediastore/image2/rss/ncsl-news.xml"

    ElseIf Request.QueryString("rssfeed") = "ncsl" Then

    strRSS_Title = "NCSL News"

    strRSS_FeedURL = "http://forms.ncsl.org.uk/mediastore/image2/rss/ncsl-news.xml"

    End If

     

    %>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <p class="txtHeader2">

    <%= strRSS_Title %></p>

    <%

    Dim xmlNews_RSS As System.Xml.XmlDocument = New System.Xml.XmlDocument

    xmlNews_RSS.Load(strRSS_FeedURL)

    Dim xdsNews_RSS As XmlDataSource = New XmlDataSource

    xdsNews_RSS.Data = xmlNews_RSS.OuterXml

    xdsNews_RSS.XPath = "/rss/channel/item"

    rptNews_RSS.DataSource = xdsNews_RSS

    rptNews_RSS.DataBind()

    %>

    <asp:Repeater ID="rptNews_RSS" runat="server">

    <ItemTemplate>

    <table class="tblGeneral" style="border-style: solid; border-width: 1px; border-color: #DFDFDF;" width="95%" align="center">

    <tr>

    <td class="cellBlank" width="100%" valign="top">

    <p align="center">

    <span class="txtHeader3"><a href="<%# XPath("link") %>">

    <%# XPath("title") %></a></span><br />

    <span style="font-size: 9px;">

    <%# XPath("pubDate") %></span></p>

    <p>

    <%# XPath("description") %></p>

    <p>

    <a href="mailto:your@friend.com?subject=<% = strRSS_Title %> Item&body=Hello!%0A%0AI just found this great news item on the Ormiston Education site: www.ormistoneducation.co.uk%0A%0ASee you soon!" style="font-size: 9px;">

    <img src="images/icons/mail.jpg" border="0" />

    Send to a Friend</a> | <a href="<% = strRSS_FeedURL %>" target="_blank">

    <img src="images/icons/rss.jpg" border="0" />

    Subscribe to RSS</a></p>

    </td>

    </tr>

    </table>

    <br />

    <br />

    </ItemTemplate>

    </asp:Repeater>

    </div>

    </form>

    </body>

    </html>

    Regards

    Simon C

  • 3 months ago

     Thanks, that sorted the showing of the "Send to a friend" and "Subscribe to RSS" but the viewing different feeds doesn't work, any ideas?

  • 2 months ago

    Hi,

    It seems that the xmldatasource can't handle feeds that are in RDF format, which is why the data doesnt change, it can't handle it.

    I found this toolkit: http://www.codeplex.com/ASPNETRSSToolkit.  If you download the binaries zip from that you are just about set to go.

    You basically put the binary file into the bin folder, register the control on the page and away you go.

    Here is a quick sample.

    <%@ Page Language="VB" %>

    <%@ Register Assembly="RssToolkit" Namespace="RssToolkit.Web.WebControls" TagPrefix="rss" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <rss:RssDataSource runat="server" ID="rssData" Url="http://forms.ncsl.org.uk/mediastore/image2/rss/ncsl-news.xml"></rss:RssDataSource>

     

    <asp:GridView runat="server" ID="gdvRSS" DataSourceID="rssData" AutoGenerateColumns="true"></asp:GridView>

     

    </div>

    </form>

    </body>

    </html>

     

    You will need to rework your current solution though.

    If I can help anymore, let me know.

    Si

  • 2 months ago

    I've used the RSS toolkit in previous projects and can highly recommend it. It's simple to use and makes life a lot easier for this sort of thing!

     

  • 2 months ago

    Sorry to be an idiot but I'm getting an...


    Error    1    Unknown server tag 'rss:RssDataSource'.    \\Visual Studio 2008\WebSites\WebSite1\Default.aspx    22   


    ... error. I'm using Visual Web Developer 2008, but this is just for testing, I will eventually just use a plain text editor, so even if you can tell me raw code?

    Thank you very much for your help so far :)

  • 2 months ago

    Hi Aaron,

    Did you download and install the rsstoolkit binary into your apps Bin folder?

    Are you using .net 1.1 or 2?

    Regards

    Simon C

  • 2 months ago

    The first thing I would do is to check that you have a declaration similar to the following at the top of your Default.aspx file:

    <%@ Register Assembly="RssToolkit" Namespace="RssToolkit.Web.WebControls" TagPrefix="rss" %>
    The other thing to check is to make sure that you've added a reference to RssToolkit.dll. 

  • 2 months ago

     Yeah did all of that, and litterally copy and pasted your examples with the "Register Assemby=..." line at the top.

     Still no luck :(

  • 2 months ago

    Did you add the RSS items into your Visual Studio toolbox? I would try dragging an RSSDataSource from the toolbox onto your aspx and to let it create the "Register Assembly=..." line for you - maybe this might do the trick. 

  • 2 months ago

     Yeah tried that just as you said, but in the toolbox it comes up as a textfile!!

     

    Very odd!

  • 2 months ago

    Yes that's very strange! The RSS toolkit comes with an example project. Out of interest, can you get that to work?

     

  • 2 months ago

     I try to run the example and I get this error:

     

    Error    1    Could not load file or assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.    \\ormiston-server\home\AaronHudson\Visual Studio 2008\WebSites\WebSite4\web.config    21    

     

    Crazy...

  • 2 months ago

    Hi,

    That sounds like you are having problems with the .net 2 framework.

    Do you have it installed properly?

    Si

     

  • 2 months ago

    Hi,

    My bad, that is the AJAX extensions.

    If you dont use AJAX, you can delete that line and it should work fine.

    Si

  • 2 months ago

    Sorry to be a spanner, but I'm using the example one still and deleting that line I've now got:

     

    Error    1    Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.    \\ormiston-server\home\AaronHudson\Visual Studio 2008\WebSites\WebSite6\web.config    24   

     

    Arhhhhhh!!!! Angry AngryStick out tongue

  • 2 months ago

    Hi,

    You could try following this link and see if it helps.

    http://support.microsoft.com/?id=320268

    Regards

    Simon C

  • 2 months ago

    Very strange. I've resided to the fact it's the .NET framework on my PC that's screwing this up, so I've used the example posted on this threat and I've uploaded the code to the server it's going to be viewed on and now I'm getting:


    Parser Error Message: Could not load file or assembly 'RssToolkit' or one of its dependencies. The system cannot find the file specified.

     

    When it comes to:

     

    Line 3:  <%@ Register Assembly="RssToolkit" Namespace="RssToolkit.Web.WebControls" TagPrefix="rss" %>
     

  • 2 months ago

    On the server that it's going to be viewed on, have you included the RSSToolkit.dll file into the bin directory of your web application? It might also be worth adding RSSToolkit.dll into the global assembly cache on that machine if it's not there.

  • 2 months ago

    Yeah it's there and in the root. How would I go about putting it in the global assembly cache?

    I can't believe how overly complex this is...

  • 2 months ago

    I know this isn't much help to you but I was lucky in that all of this worked first time for me when I used the RSSToolkit!

    You can add the dll into the GAC by simply copying it into C:\Windows\Assembly (or wherever it is that you have Windows installed) I hope this does the trick for you! 

  • 2 months ago

    Always me who has problems Crying

    I've just tried to add the dll to the assemby folder and it came back with this error: "Assemby 'RssToolkit.dll' is required to be strongly named."

    Now I am lost...

  • 2 months ago

    I've tried doing the same and I get the same error - they haven't strongly named the dll! 

    I've done a quick search and have found the following on the RSS Toolkit 'issue tracker'. On the page are some instructions on how to strongly name the assembly. 

    http://www.codeplex.com/ASPNETRSSToolkit/WorkItem/View.aspx?WorkItemId=11231 

     
     

  • 2 months ago

    I've taken a look at this but it doesn't seem to be available to me in Visual Web Developer :(

    Any ideas?

    Thank you :)

  • 2 months ago

    If you still need a strongly named copy of this dll, I've saved a copy here:

    http://dotnettim.spaces.live.com/blog/cns!4B800EB59FAEDC2A!254.entry

     

Post a reply

Enter your message below

Sign in or Join us (it's free).