Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2740 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET AJAX > How to consume other Sites RSS in ASP.NET

How to consume other Sites RSS in ASP.NET

Article posted by Vuyiswamb on 12/2/2009 | Views: 4538 | Category: ASP.NET AJAX | Level: Beginner red flag


One might wonder how they can have news on their website. Will they have to be there and Update their site to get updated content? Do they have to hunt for news? This becomes confusing for a content designers of some websites. Can they get this news from other good Source? But how will they update the News from those sources? These are questions that are asked every day. In this article I will show you how to use RSS from other sites and display them in your site and get updates without you being there. Your website can display news with images and links to the source in ASP.Net


Introduction

One might wonder how they can have news on their website. Will they have to be there and Update their site to get updated content? Do they have to hunt for news? This becomes confusing for a content designers of some websites. Can they get this news from other good Source? But how will they update the News from those sources? These are questions that are asked every day. In this article I will show you how to use RSS from other sites and display them in your site and get updates without you being there. Your website can display news with images and links to the source in ASP.Net

Background

In this Article we are going to consume RSS feeds in our ASP.NET website

Using the code

We are going to user C# as our language.

Start

Open Visual Studio and Create a New Website. Automatically you will have an empty page defined for you like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 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></title>

</head>

<body>

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

    <div>

   

    </div>

    </form>

</body>

</html>

 

Go to Design View and you will notice there is nothing on your page. Now open your Toolbox and add a Gridview Control in your page as depicted in the Diagram below.

And after you have added these two Controls you should have something like this in your mark-up.

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 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:GridView ID="GridView1" AutoGenerateColumns="true" runat="server">

        </asp:GridView>

    </div>

    </form>

</body>

</html>

 

 


The Next Step is to get a RSS Url. Well almost every site has RSS and I have one that I will use as my example

http://feeds.feedburner.com/SdTimesLatestNews

Double click on your page and you will see the page load. And write the following in the page load event.

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using  System.Xml;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        XmlTextReader readerSport = new                   XmlTextReader("http://www.sowetan.co.za/rss/Sowetan_TopStories.xml");

 

        DataSet dsSport = new DataSet();

 

        dsSport.Clear();

 

        dsSport.ReadXml(readerSport);

 

        GridView1.DataSource = dsSport.Tables[2];

 

        GridView1.DataBind();

    }

}

This code will Bind the Grid on page load and show us the Contents of the RSS. Lets test this and see how it looks. Press f5 and you will see this


 

This is Good info but not for our users. This info contains more than what we need some is info that we don’t need. The nice thing about this RSS example is that it has images, that means our news will show images not only text. Visitors like to see images. Writing a Statement like “Obama Visits South Africa” that has no image is different from a the stament that read “Omaba Visits South Africa” and a Pic of Obama near the statement. A user becomes interested in a statement that has a Photo near it. In this RSS we need the “url”, “Title”, and we can just redirect the users to the site itself. Lets see how can we do this in our grid. On your markup change the grid properties and add more and make sure it looks like this

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 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:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">

        <Columns>

        <asp:TemplateField >

        <ItemTemplate>

        <asp:Image ID = "myimage" runat="server" ImageUrl='<%# Eval("Url")%>' />

        </ItemTemplate>

        </asp:TemplateField>

        <asp:TemplateField HeaderText ="Story">

        <ItemTemplate>

        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title")%>'></asp:Label>       

        </ItemTemplate>

        </asp:TemplateField>

        <asp:TemplateField HeaderText ="Read more">

        <ItemTemplate>

        <asp:HyperLink ID="readmore" runat="server" Text="Read more..." NavigateUrl='<%# Eval("Link")%>'></asp:HyperLink>

        </ItemTemplate>

         </asp:TemplateField>

        </Columns>

        </asp:GridView>

    </div>

    </form>

</body>

</html>

 

If you run this you will see the Following

 


The Images changes, maybe the time you try this example there will be a Different images and Story. If you have this on your website, your website will updates itself you wont need to look for news.

Conclusion

The Solution was simple and clear. This is how we consume other websites RSS

Thank you for visiting DotnetFunda.

 

Vuyiswa Maseko

 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:8 year(s)
Home page:http://www.Dotnetfunda.com
Member since:Sunday, July 06, 2008
Level:NotApplicable
Status: [Member] [MVP] [Administrator]
Biography:Vuyiswa Junius Maseko is a programmer and a moderator in ".NetFunda. Vuyiswa has been developing for 8 years now. his major strength are C# 1.1,2.0,3.0,3.5 and sql and his interest are in Silverlight,WPF,C#. He has been doing a lot of Silverlight development. He has been using .net since the beta version of it. He is also an online Trainer at www.Itfunda.com. Thanks to people like Sheo Narayan (.Netfunda) , Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject),.They have made vuyiswa what he is today.
>> Write Response - Respond to this post and get points
Related Posts

This article describes the solution of the problem in which jQuery doesn't work for elements placed under ASP.NET AJAX UpdatePanel when partial postback is done.

This article describes how to filter GridView Records using AJAX Slider Control.

Every Article I write is like a story. There are a lot of Companies that provide readymade controls that skinable and make out GUI (Graphical User interface) looks good. Spending time inventing a Wheel is not a Good idea. A lot of Developers or Development houses should look at Infragistics (www.Infragistics.com), ComponentArt and Telerik. There are more of them but these are Microsoft partners and they have made their mark in providing readymade Controls that you can use in your Visual Studio. But there are times where we don’t have a Budget and we been Controls like Date Picker. Well if you check the Visual Studio Toolbox, there is no Such Control.

Hi In this article we will look some very nice features provided by Asp.net ajax library.

In this article we are going to see how to access the webservice from Sys.Net.WebRequest where we can find the country by entering the IP address.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:27:43 AM