c# - How to retrieve a blog/website recent posts from other blogs -
how retrieve blog/website recent posts other blogs http://www.indiblogger.in
get recent post websites or blogs , store db ... how achieve in asp.net c#
the recipe answer question is:
- retrieve feeds
- create database context
- create entity class
- create dbset
- add feeditems dbset
- create webpage
- add gridview , button
- add code in codebehind
retrieve feeds
based on link first step find can find feed of posts enable retrieve those. feed url provided feedburner.
next use load method of syndicationfeed. method accepts xmlreader unfortunately date format used in rssfeed not understood have use helper provided 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 database use entityframework 6 (use nuget install)
for codefirst approach create dbcontext derived class , property posts
class blogcontext:dbcontext { public dbset<post> posts { get; set; } }
and class hold post data.
public class post { public int id {get; set;} public string body { get; set;} }
main functionality
combine load of datafeeds , database storage. iterate on feed.items, create post item data , add each post posts colection on datacontext. don't forget call savechanges
store in 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 posts in webpage bind result of posts collection gridvew
markup in 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 blogcontext once more , call tolist() on posts data-bindable list of post objects. gridview has knowledge render list in html page.
protected void grid_load(object sender, eventargs e) { var db = new blogcontext(); gridview1.datasource = db.posts.tolist(); gridview1.databind(); }
if button clicked load feeds in database:
protected void button1_click(object sender, eventargs e) { loadfeedandstore(); }
Comments
Post a Comment