Question

How to retrieve a blog/website recent posts from other blogs like http://www.indiblogger.in

get recent post from any websites or blogs and store this into my DB ... How to achieve this in asp.net c#

Was it helpful?

Solution

The recipe to answer your question is:

  1. Retrieve Feeds
  2. Create a Database Context
    1. Create an Entity class
    2. Create DbSet
  3. Add feeditems to the DbSet
  4. Create a webpage
    1. Add GridView and Button
    2. Add code in codebehind

Retrieve Feeds

Based on your link the first step is to find where we can find the feed of posts that would enable us to retrieve those. The feed url is provided by feedburner.
Next we use the Load method of SyndicationFeed. That method accepts an XmlReader but unfortunately the Date format used in the rssfeed is not understood so we have to use a helper provided by Micorosft:

XmlReader Helper

    class MyXmlReader : XmlTextReader
    {
        private bool readingDate = false;
        const string CustomUtcDateTimeFormat = "ddd MMM dd HH:mm:ss Z yyyy"; // Wed Oct 07 08:00:07 GMT 2009

        public MyXmlReader(Stream s) : base(s) { }

        public MyXmlReader(string inputUri) : base(inputUri) { }

        public override void ReadStartElement()
        {
            if (string.Equals(base.NamespaceURI, string.Empty, StringComparison.InvariantCultureIgnoreCase) &&
                (string.Equals(base.LocalName, "lastBuildDate", StringComparison.InvariantCultureIgnoreCase) ||
                string.Equals(base.LocalName, "pubDate", StringComparison.InvariantCultureIgnoreCase)))
            {
                readingDate = true;
            }
            base.ReadStartElement();
        }

        public override void ReadEndElement()
        {
            if (readingDate)
            {
                readingDate = false;
            }
            base.ReadEndElement();
        }

        public override string ReadString()
        {
            if (readingDate)
            {
                string dateString = base.ReadString();
                DateTime dt;
                if(!DateTime.TryParse(dateString,out dt))
                    dt = DateTime.ParseExact(dateString, CustomUtcDateTimeFormat, CultureInfo.InvariantCulture);
                return dt.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture);
            }
            else
            {
                return base.ReadString();
            }
        }
    }

FeedReader

SyndicationFeed LoadFeed()
{
    return SyndicationFeed.Load(
     new MyXmlReader(@"http://feeds.feedburner.com/indiblogger?format=xml")
      );
}

Database

For the database use EntityFramework 6 (use Nuget to install)

For a codefirst approach create a DbContext derived class and a property for your Posts

class BlogContext:DbContext
{
    public DbSet<Post> Posts { get; set; } 
}

And a class to hold your Post data.

public class Post
{
    public int Id {get; set;}
    public string Body { get; set;}
}

Main functionality

Combine the load of the datafeeds and the database storage. Iterate over the feed.Items, create a Post from the item data and add each Post to the Posts colection on the DataContext. Don't forget to call SaveChanges to store everything in the database.

public void LoadFeedAndStore()
{
   var feed = LoadFeed();
   var db = new BlogContext();

    foreach(var item in feed.Items)
    {
       db.Posts.Add(
        new Post { 
            Body = item.Summary == null ? "<empty>":  item.Summary.Text 
            });
    }
    db.SaveChanges();
}

Show in asp.net page

To show the posts in a webpage bind the result of the Posts collection to a gridvew

Markup in the page

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="GridView1" runat="server" OnLoad="Grid_Load" >
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" Text="Load Feed" OnClick="Button1_Click"/>
</asp:Content>

Code Behind

We use the BlogContext once more and call ToList() on the Posts to get a data-bindable list of Post objects. The GridView has all the knowledge to render this list in the html page.

protected void Grid_Load(object sender, EventArgs e)
{
    var db = new BlogContext();
    GridView1.DataSource = db.Posts.ToList();
    GridView1.DataBind();
}

If the button is clicked we load the feeds in the database:

protected void Button1_Click(object sender, EventArgs e)
{
    LoadFeedAndStore();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top