Back

Displaying a BBC News RSS Feed using ASP.net/C#

The BBC now encourages the use of their RSS news feeds on your own site as long as you use the proper format and attribution, although you are not permitted to use the BBC logo on your own site (see the BBC News RSS guide). 

In this particular article we will look at creating an RSS feed for display on your own web site, as for example the technology news feed displayed on the front of this site.  The code provided will also cache the data that we don't keep having contact the Beeb on every page impression.

In this particular example we are going to create an RSS feed in an ascx user control so that it can then be dragged and dropped into any page on the web site.  We will use a datagrid, although any control will suffice such as a literal or a repeater. 

  1. To begin with create your ascx in Visual Studio, then in the code view you will need to reference the XML namespace at the top, as per the following:

    using System.Xml;
     
  2. Next we are going to create a method which creates a datatable and then populates this datatable with the data we want from the RSS feed.  The code for the method looks like this:

    private DataTable GetRSSFeed()
      {
       // Create a datatable to store the xml data
       DataTable dt = new DataTable();
       dt.Columns.Add(new DataColumn("title", typeof(String)));
       dt.Columns.Add(new DataColumn("link", typeof(String)));
       DataRow dr;

       // create an xml document
       XmlDocument document = new XmlDocument();
       document.Load("
    http://newsrss.bbc.co.uk/rss/
    newsonline_uk_edition/technology/rss.xml
    ");
       XmlNodeList titles = document.GetElementsByTagName("title");
       XmlNodeList links = document.GetElementsByTagName("link");
       
       // transfer xml document to the datatable
       for (int i = 2; i<8; i++)
       {
        dr = dt.NewRow();
        dr[0] = titles[i].InnerText;
        dr[1] = links[i].InnerText;
        dt.Rows.Add(dr);               
       }
             
       return dt; 
       
      }
     
    As you can see in the code above, after creating the datatable we then create two columns in it, one for the news title and one for the url for the news article on the BBC site. 

    Then we create an XML document and load the BBC rss news feed into this document.

    We then want to iterate through each row in the XML document using the for loop and populate the data we find into the empty datable.  You will see from the loop it starts at row 2 and finishes at row 8.  The reason is the first two rows of the BBC RSS just contain some information about the news being displayed, for example Education, Technology etc.  It ends at row 8 so we can just display the number of news items we want, rather than the whole lot - you can change this as you require.
     
  3. The next thing we are going to do is create a datagrid called dgNews in our aspx page to load the datatable into.  The code looks like this:

     CssClass="ticker" runat="server" AutoGenerateColumns="False" CellSpacing="0">
     
      
       
        ">
         <%# DataBinder.Eval(Container.DataItem, "title") %>
        

       

      

     



    As you can see we specify the column names as required.
     
  4. Finally in our page load event in the .cs file we need to call the GetRSSFeed method to create the datatable.  Firstly though we check to see whether or not the data is already in the cache - we only make the call to the BBC site if it isn't in the cache.  As you will see the cache is only refreshed every 20 minutes.

      private void Page_Load(object sender, System.EventArgs e)
      {  
       // check to see if the cache is empty, if it is then
       // fill it with the news feed items
       if (Cache["BBCRSS"] == null)
       {
        try
        {
         Cache.Insert("BBCRSS", GetRSSFeed(),
          null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
        }
        catch
        {
         dgNews.Visible = false;
        }
        
       }
               
       dgNews.DataSource = Cache["BBCRSS"];
       dgNews.DataBind();
      }

To download a code listing in zip format click here.

 

Back


Make a Comment on this Article

Your Name:
Comment: