Friday, October 3, 2014

RSS feed C#

11:12 AM - By azeheruddin khan 0

This tutorial will demonstrate how to get data from RSS feed and bind data to a Repeater control with ASP.NET 4.0 and C#.

RssFeed.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RSSFeed
{
    public class RssFeed
    {
        /// <summary>
        /// Gets or sets the term
        /// </summary>
        public string Term { get; set; }

        /// <summary>
        /// Gets or sets the name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the uri
        /// </summary>
        public string Link { get; set; }

        /// <summary>
        /// Gets or sets the published date
        /// </summary>
        public string Published { get; set; }

        /// <summary>
        /// Gets or sets the updated date
        /// </summary>
        public string Updated { get; set; }


        /// <summary>
        /// Gets or sets the edited date
        /// </summary>
        public string Edited { get; set; }


        /// <summary>
        /// Gets or sets the title
        /// </summary>
        public string Title { get; set; }


        /// <summary>
        /// Gets or sets the origLink
        /// </summary>
        public string OrigLink { get; set; }     


        /// <summary>
        /// Gets or sets the content_Text
        /// </summary>
        public string Content_Text { get; set; }
       
        
    }
}

RssHandler.cs


using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;

namespace RSSFeed
{
    public static class RssHandler
    {
       
        public static List<RssFeed> ReadFeed(string url)
        {
            //create a new list of the rss feed items to return
            List<RssFeed> rssFeedItems = new List<RssFeed>();
              

            //create an http request which will be used to retrieve the rss feed
            HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(url);

            //use a dataset to retrieve the rss feed
            using (DataSet rssData = new DataSet())
            {
                //read the xml from the stream of the web request
               
                rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());               

                var p = from t in rssData.Tables["category"].AsEnumerable()
                             join t1 in rssData.Tables["Author"].AsEnumerable() on t.Field<int?>("entry_id") equals t1.Field<int?>("entry_id")
                             join t2 in rssData.Tables["entry"].AsEnumerable() on t1.Field<int?>("entry_id") equals t2.Field<int?>("entry_id")
                             join t3 in rssData.Tables["content"].AsEnumerable() on t2.Field<int?>("entry_id") equals t3.Field<int?>("entry_id")
                             select new
                             {
                                 Term = t.Field<string>("term"),
                                 Name = t1.Field<string>("name"),
                                 Link = t1.Field<string>("uri"),
                                 Published = t2.Field<string>("published"),
                                 Updated = t2.Field<string>("updated"),
                                 Edited = t2.Field<string>("edited"),
                                 Title = t2.Field<string>("title"),
                                 OrigLink = t2.Field<string>("origLink"),
                                 Content_Text = t3.Field<string>("content_Text")

                             };

                foreach (var m in p)
                {
                    rssFeedItems.Add(new RssFeed
                    {
                        Term =m.Term,
                        Name = m.Name,
                        Link = m.Link,
                        Published = m.Published,
                        Updated = m.Updated,
                        Edited = m.Edited,
                        Title = m.Title,
                        OrigLink = m.OrigLink,
                        Content_Text = m.Content_Text

                    });

                }

            }

            //return the rss feed items
            return rssFeedItems;
        }
    }
}

Feed.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Feed.aspx.cs" Inherits="RSSFeed.Feed" %>

<!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 style="margin: 0; width: 800px; clear: both;">
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <div>
                            <table>
                                <tr>
                                    <td colspan="2">
                                        <%# Eval("Title") %>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label>Author:</label>
                                    </td>
                                    <td>
                                        <%# Eval("Name") %>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                    <label>Author Profile:</label>                                   
                                    </td>
                                    <td>
                                         <%# Eval("Link")%>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                    <label>Posted Date:</label>                                    
                                    </td>
                                    <td>
                                         <%# Eval("Published")%>
                                    </td>
                                </tr>
                                </tr>
                                <tr>
                                     <td>
                                     <label>Orginal Link:</label>
                             
                                    </td>
                                    <td>
                                         <%# Eval("OrigLink")%>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div>
                            <%# Eval("Content_Text") %>
                        </div>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Feed.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RSSFeed
{
    public partial class Feed : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.DataSource = RssHandler.ReadFeed("http://feeds.feedburner.com/CodingWriter");
            Repeater1.DataBind();
        }
    }
}





About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top