Get Blogger Post - Rss Feed in our website

Task: Get Blogger RSS Feed/Post to our website (Show blogger post to our website).

Description: Sometime we need to show blogger post(RSS Feed) to our website. Using below code we can read all blog post from blogger(blogspot) Or we can also modify the code for reading other RSS feed(XML). The basic behind the reading RSS is read XML response. The blogger provides us a Rss link, from where we can read the XML.
I also used the string trimming with removing html tag Method As I used previously : Remove Html Tag
Here I used two methods for achieving this.

Method 1 : Using XML...

// Code Start //

using System.Xml;

using System.Text.RegularExpressions;

public string blogdetail = "";
private void ReadRssFeed()
{
    string blog = "";
    string RssFeedUrl = "http://hemantrautela.blogspot.com/feeds/posts/default?alt=rss";
    XDocument xDoc = new XDocument();
    xDoc = XDocument.Load(RssFeedUrl);
    var items = (from x in xDoc.Descendants("item")
                 select new
                 {
                     title = x.Element("title").Value,
                     link = x.Element("link").Value,
                     pubDate = x.Element("pubDate").Value,
                     description = x.Element("description").Value
                 });
    if (items != null)
    {
        foreach (var i in items)
        {
            String result = Regex.Replace(i.description, @"<[^>]*>", String.Empty);
            if (result.Length > 300)
            {
                result = result.Substring(0, 300);
                result = result.Substring(0, result.LastIndexOf(" ")) + "...";

            }
            blog += "<a href='" + i.link + "' rel='nofollow'>" + i.title + "</a><br/>" +
                    result + "<br/><br/>";
        }
        blogdetail = blog;
    }

}

// Code End//


Method 2 : Using by System.ServiceModel.Syndication

// Code Start //

    
    using System.Text.RegularExpressions;

    public string blogdetail = "";
    public void ReadRssFeed()
    {
        string rssFeedUrl = "http://hemantrautela.blogspot.com/feeds/posts/default?alt=rss";
        var syndicationFeed = System.ServiceModel.Syndication.SyndicationFeed.Load(XmlReader.Create(rssFeedUrl));
        string blog = "";
        foreach (var item in syndicationFeed.Items)
        {
            String result = Regex.Replace(item.Summary.Text, @"<[^>]*>", String.Empty);
            if (result.Length > 300)
            {
                result = result.Substring(0, 300);
                result = result.Substring(0, result.LastIndexOf(" ")) + "...";
            }
            blog += "<a href='" + item.Links[0].Uri + "' rel='nofollow'>" + item.Title.Text + "</a><br/>" +
                    result + "<br/><br/>";
        }
        blogdetail = blog;

    }

// Code End//

No comments:

Post a Comment