C# - RSS Feed Reader with ASP.NET

A while ago I was tasked with creating a website whereby a website could pickup rss feeds which were listed in a database and then grab the RSS feed form the URL parse it and display it on the page, this is the code that I came up with which is kinda basic, but...it works!

This first snippet shows in C# how the the latest 10 to be added RSS feeds were generated and written to the page, it also shows the additional search functionality which awarded me extra merits for placing additional functionality into the project, I will note it out in the code to explain each part.

feeds.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
 
//Opens the connection using the SQL method below
//NB: if using this live, you will need password attribute.
string Conn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
"|DataDirectory|\
db.mdf;Integrated Security=True;User" +
"Instance=True"
;

using (SqlConnection con = new SqlConnection(Conn))
{
con.Open();
//selects the feeds, latest first.
string sql = "select * from RSS order by ID desc";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
int count = 0;
 
string resultlist = "";
string url = "";
string id = "";
string name = "";
//loops through the results from the SQL
while (reader.Read() && count < 10)
{
id = reader[0].ToString();
url = reader[1].ToString();
name = reader[2].ToString();
count++;
//concatonates the results info into the list
resultlist = resultlist + "
"
+ count + ". "
+ name +
"(" + id + ")&lt;br /&gt;" + url +
"
&lt;br /&gt;&lt;hr /&gt;";

}
 
resultsinfo.InnerHtml = "Most recent 10 RSS feeds added:";
results.InnerHtml = resultlist;
reader.Close();
 
con.Close();
}
 
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string Search = txtSearch.Text;
//Opens the connection using
string Conn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
"|
DataDirectory|\
db.mdf;Integrated Security=True;User" +
"Instance=True";


using (SqlConnection con = new SqlConnection(Conn))
{
con.Open();
 
string sql = "select * from RSS where name like '%" +
Search + "%'
OR url like '%" + Search + "%'";

SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
int count = 0;
 
string resultlist = "";
while (reader.Read())
{
string url = reader[1].ToString();
string name = reader[2].ToString();
count++;
//this line is the main difference to the above
//section except the actual SQL,
this replaces
//any instances of the search term with a bold version.

resultlist = resultlist + "&lt;br /&gt;" + count + ". " +
name.Replace(Search, "
" +
Search + "") + "" +
url.Replace(Search, "
" + Search +"") +
"
&lt;br /&gt;&lt;hr /&gt;";

}
 
resultsinfo.InnerHtml = "Found " + count +
"
results for search term """ + Search + """;

results.InnerHtml = resultlist;
reader.Close();
 
con.Close();
}
}
}
 


As you can see above it looks and sounds complicated to do the db connection and run it, but it really isn't the real tricky bit of the website was, infact getting the RSS feed and making it parseable, luckily on my hunting for solutions to this problem I came across this: showFeed it basically shows the feed for you, so simply enough grabbing the URL and then handing it to the class which displays it for you! Done!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string feedID = Request.QueryString["id"];
//Opens the connection using
string Conn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
"|DataDirectory|\db.mdf;Integrated Security=True;User" +
"Instance=True"
;

using (SqlConnection con = new SqlConnection(Conn))
{
con.Open();
 
string sql = "select * from RSS where id='" + feedID + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
string feedURL = "";
while (reader.Read())
{
feedURL = reader[1].ToString();
}
showfeed.InnerHtml = feedURL;
reader.Close();
 
con.Close();
}
}
}



thats a fully working C# with ASP.NET functions for listing RSS feeds and then displaying RSS feeds on a .aspx page hope it helps anyone out there trying to get this working! Remember to supply feedback!

ASP.NET

Buy me a beer or a book?

amazon PayPal - The safer, easier way to donate online!

Free Downloads