Get RSS Feed in ASP.Net

Introduction

This article explains how to get live updates in our website or webpage using RSS Feeds. It's very easy to get RSS Feeds in our custom page or our website.

There are the following links that can be used to get updates of C# Corner:

You can find this link in www.c-sharpcorner.com as described in the following image.



When you click there a new window opens with the XAML data.

Now open Visual Studio and follow the procedure that I am explaining here and get the live update.

Step 1

Create a web project by clicking on new project.

Step 2

Go to code behind and write the following function to read the XAML data from the *.aspx page:

  1. public XElement ReadXmlFromCShrpCornerPage(string CsharpCornerUrl)    
  2. {    
  3.      HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(CsharpCornerUrl);    
  4.      WebResponse myResponse;    
  5.      myResponse = myRequest.GetResponse();    
  6.      XmlDocument _xmlDoc = new XmlDocument();    
  7.      using (Stream responseStream = myResponse.GetResponseStream())    
  8.           {    
  9.              _ xmlDoc.Load(responseStream);    
  10.           }    
  11.           return XElement.Load(new XmlNodeReader(_xmlDoc));    
  12. }   
Step 3

Now using the preceding XElement create an HTML string using the following function.
  1. public string CshrpCornerLiveUpdateDiv(string CsharpCornerUrl)    
  2. {   StringBuilder sb = new StringBuilder();      
  3.     var dox = ReadXmlFromCShrpCornerPage(CsharpCornerUrl);     
  4.     //Description about page heading       
  5.     var Headting = dox.Descendants("channel").Select(d =>           
  6.     new   
        {             
  7.         Title = d.Element("title").Value,             
  8.         Description = d.Element("description").Value,     
  9.         Link = d.Element("link").Value,           
  10.     }).ToList();      
  11.    //list of daily updated item in c-corner       
  12.    var UpdatedList = dox.Descendants("item").Select(d =>           
  13.    new   
       {             
  14.         Title = d.Element("title").Value,           
  15.         Description = d.Element("description").Value,             
  16.         Link = d.Element("link").Value,            
  17.         PubDate = d.Element("pubDate").Value,          
  18.     
  19.         AuthorName = d.Element("author").Value        
  20.    }).ToList()    
  21.   
  22.    foreach (var PageHeading in Headting)    
  23.    {    
  24.       sb.Append(" <div class=\"page\"style=\"outline: 0px; width: 1024px; margin: 0px auto; color: rgb(141, 141, 141); font-   
          family: Calibri, 'Segoe UI', Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; 
          letter-spacing: normal; line-height: normal;orphans: auto; text-align: start; text-indent: 0px; text-transform: 
          none; white-space: normal;widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;\">"
     +"
          <div class=\"main\"style=\"outline: 0px; width: 1024px; float: left; margin-top: 5px; min-height: 430px; z-
          index: 0; height: auto !important; background-color: rgb(255, 255, 255);\">"
     +
          <div class=\"leftCntr\" style=\"outline: 0px; width: 676px; float: left;\">
          <div class=\"recentactivityBox\"style=\"outline: 0px; width: 676px; overflow: hidden; margin: 10px 0px 0px;\">"
     +"
          <h2 class=\"HeadingBold\" style=\"outline: 0px; color: rgb(85, 85, 85);font-size: 26px; margin: 0px; padding: 0px; font-
          weight: 400; text-transform: uppercase; font-family: BebasNeueRegular;\">"
     + PageHeading.Title + "</h2>
          <li style=\"outline: 0px; padding: 4px 0px 0px; display: block; overflow: hidden;>\""
    );    
  25.    }    
  26.   
  27.    //here change the conduction and increase the list of update   
  28.   
  29.    for (int i = 0; i < 5; i++)//UpdatedList.Count;    
  30.    {    
  31.       sb.Append("<div class=\"left\" style=\"outline: 0px; width: 615px; float: clasleft; line-height: 20px;\">" +"
          <h3 style=\"outline: 0px; font-size: 17px; color: rgb(51, 51, 51); font-family: Calibri; font-
          weight: 400; margin: 0px; padding: 0px;\">"
     +  "<a href=\"" + UpdatedList[i].Link + "\"" +"style=\"outline: 0px; text-
          decoration: none; cursor: pointer; color: rgb(51, 51, 51);\">"
     +"" + UpdatedList[i].Title + "</a></h3>" +"By<span class=\"Apple-
          converted-space\"> </span><a class=\"LinkRed\""
     + "href=\"http://www.c-sharpcorner.com/authors/ee01e6/manish-kumar-
          choudhary.aspx\""
     +"style=\"outline: 0px; text-decoration: none; cursor: pointer; color: rgb(255, 102, 0); font-
          family: Calibri; font-weight: 400;\">"
     + "" + UpdatedList[i].AuthorName + "<span class=\"Apple-converted-space\"> </span></a>
          <span"
     +"class=\"Apple-converted-space\"> </span> On " +"" + UpdatedList[i].PubDate + "</div>");    
  32.    }    
  33.     
  34.    sb.Append("</li></ul></div></div></div>");    
  35.    }    
  36.    return sb.ToString();    
  37. }    
Note: One can modify the CSS of this function if required for their own design and theme. Here basically I am using the C# Corner CSS design.

Step 4

Now go to design mode and create a HTML server control div where we display the content of the latest update.
  1. <div id="CsharpBlog" runat ="server" > </div>    
Step 5

In the page load events of this page call the preceding function and bind the string with the CsharpBlog div.
  1. CsharpBlog.InnerHtml = CshrpCornerLiveUpdateDiv("http://www.c-sharpcorner.com/RSS/Blogs.aspx");   
Note: In the above function call just change the URL and get a different live update like articles, blogs and so on.



When this page is run it will display like the preceding.

Wow, we did it. Now we are getting the live update of C# Corner in our page. For a better understanding of this download the source code is attached with this article.

Summary

In this illustration you came to understand how to show a live update in our ASP.NET page.
 


Similar Articles