In this article I will explain with an example, how to fetch and display RSS Feeds in ASP.Net using C# and VB.Net.
This article covers how to fetch RSS Feeds from a URL and display it on the Website in ASP.Net using C# and VB.Net.
The RSS Feeds returned from the API
The following RSS Feed are used in this article.
HTML Markup
The following HTML Markup consists of:
Repeater: – For displaying RSS Feeds.
Repeater consists of an ItemTemplate which consists of:
Controls
HyperLink – For navigating to the Feed.
Label – For displaying the description of the Feed.
<asp:Repeater ID="rssRepeater" runat="server">
<ItemTemplate>
<table width="100%">
<tr>
<td>
<asp:HyperLink ID="lnkTitle" runat="server" Font-Bold="true"
NavigateUrl='<%#Eval("Link")%>' Text='<%#Eval("Title")%>'></asp:HyperLink>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblDescription" runat="server" Text='<%#Eval("Description")%>'></asp:Label>
</td>
</tr>
</table>
<br/>
</ItemTemplate>
</asp:Repeater>
Property Class
The class consists of following properties which will be used for deserialization of XML data.
C#
[XmlType("rss")]
public class Rss
{
[XmlElement("channel")]
public Channel Channel { get; set; }
}
[XmlType("channel")]
public class Channel
{
[XmlElement("item")]
public List<Item> Items { get; set; }
}
public class Item
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("link")]
public string Link { get; set; }
[XmlElement("pubDate")]
public string PublishedDate { get; set; }
}
VB.Net
<XmlType("rss")>
Public Class Rss
<XmlElement("channel")>
Public Property Channel As Channel
End Class
<XmlType("channel")>
Public Class Channel
<XmlElement("item")>
Public Property Items As List(Of Item)
End Class
Public Class Item
<XmlElement("title")>
Public Property Title As String
<XmlElement("description")>
Public Property Description As String
<XmlElement("link")>
Public Property Link As String
<XmlElement("pubDate")>
Public Property PublishedDate As String
End Class
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Xml;
using System.Net;
using System.Net.Http;
using System.Xml.Serialization;
VB.Net
Imports System.IO
Imports System.Xml
Imports System.Net
Imports System.Net.Http
Imports System.Xml.Serialization
Fetching and displaying RSS Feeds
Inside the Page_Load event handler,first the Security Protocol is set.
Then, the RSS Feeds API is called using HttpClient class and the response is received using GetAsync method of HttpResponseMessage class.
If the response is true then, XML string is downloaded using ReadAsStringAsync method of StringReader class.
Then, an object of XmlReader class is created using Create method of XmlReader class.
The XML string is deserialized into Rss class using Deserialize method of the XmlSerializer class.
Finally, the Rss class Items are assigned to the DataSource property of the Repeater and the Repeater is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Setting TLS 1.2 protocol.
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync("https://asp-umbraco.azurewebsites.net/rss/content").Result;
if (response.IsSuccessStatusCode)
{
using (StringReader stream = new StringReader(response.Content.ReadAsStringAsync().Result))
{
using (XmlReader reader = XmlReader.Create(stream))
{
XmlSerializer serializer = new XmlSerializer(typeof(Rss));
Rss rss = (Rss)serializer.Deserialize(reader);
rssRepeater.DataSource = rss.Channel.Items;
rssRepeater.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Not Me.IsPostBack Then
'Setting TLS 1.2 protocol.
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim client As HttpClient = New HttpClient()
Dim response As HttpResponseMessage = client.GetAsync("https://asp-umbraco.azurewebsites.net/rss/content").Result
If response.IsSuccessStatusCode Then
Using stream As StringReader = New StringReader(response.Content.ReadAsStringAsync().Result)
Using reader As XmlReader = XmlReader.Create(stream)
Dim serializer As XmlSerializer = New XmlSerializer(GetType(Rss))
Dim rss As Rss = CType(serializer.Deserialize(reader), Rss)
rssRepeater.DataSource = rss.Channel.Items
rssRepeater.DataBind()
End Using
End Using
End If
End If
End If
End Sub
Screenshot
Demo
Downloads