Rss Feed

  • Thread starter Thread starter Sonal
  • Start date Start date
S

Sonal

Hi
I am trying to provide infrastructure for RSS feeds on my Website. I
dont intend to build any news aggregators. I just want my website to
be enabled for providing RSS Feeds. I am using ASP.NET and C#. And I
dont know where and how to begin. Do you know of any tutorials that
may help me do that?
Thanks,
Sonal.
 
There's nothing special you need to do to enable a website other than
developer the code to create or consume XML.

If you intend to create feeds that can be validated you should make sure
your hosting provide has configured IIS to support the application/xml MIME
Type for .rss, .rdf, and .xml file types.
 
Sonal said:
Hi
I am trying to provide infrastructure for RSS feeds on my Website. I
dont intend to build any news aggregators. I just want my website to
be enabled for providing RSS Feeds. I am using ASP.NET and C#. And I
dont know where and how to begin. Do you know of any tutorials that
may help me do that?
Thanks,
Sonal.

Here is a simple ASPX example, the NewsDAL implemenation is left as an
exercise for the reader :)



<!-- RSS.ASPX -->
<%@ Page language="vb" ContentType="text/xml" Codebehind="rss.aspx.vb"
AutoEventWireup="false" Inherits="news.rss" EnableSessionState="False"
enableViewState="False" buffer="True"%>
<asp:Repeater id="rptRSS" runat="server">
<HeaderTemplate>
<rss version="2.0">
<channel>
<title><%=ChannelTitle%></title>
<link><%=ChannelLink%></link>
<description>
<%=ChannelDescription%>
</description>
</HeaderTemplate>

<ItemTemplate>
<item>
<title><%# FormatForXML(DataBinder.Eval(Container.DataItem, "Title"))
%></title>
<description><%# FormatForXML(DataBinder.Eval(Container.DataItem,
"Description")) %></description>
<link>http://<%=Request.ServerVariables("SERVER_NAME") &
Request.ApplicationPath %>/Story.aspx?ID=<%#
DataBinder.Eval(Container.DataItem, "ArticleID") %></link>
<author><%# FormatForXML(DataBinder.Eval(Container.DataItem, "Author"))
%></author>
<pubDate><%# String.Format("{0:R}", DataBinder.Eval(Container.DataItem,
"DatePublished")) %></pubDate>
</item>
</ItemTemplate>

<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>


' rss.aspx.vb
Imports System.Xml

Public Class rss
Inherits System.Web.UI.Page
Public ChannelTitle As String = ""
Public ChannelLink As String = ""
Public ChannelDescription As String = ""

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Protected WithEvents rptRSS As System.Web.UI.WebControls.Repeater

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ArticleCount As Integer
Dim ChannelID As Integer
Dim data As NewsDAL.Data = New NewsDAL.Data(Global.ConnString)

Try
Try
ArticleCount = Integer.Parse(Request.QueryString("Count"))
Catch
ArticleCount = 5
End Try
Try
ChannelID = Integer.Parse(Request.QueryString("ChannelID"))
Catch
ChannelID = 1
End Try

Dim ds As New DataSet
data.FillRSSChannels_Get(ds, ChannelID)
Dim dr As DataRow = ds.Tables(0).Rows(0)
ChannelTitle = FormatForXML(dr("Title").ToString())
ChannelLink = FormatForXML(dr("Link").ToString())
ChannelDescription = FormatForXML(dr("Description").ToString())
Catch ex As Exception
Response.Clear()
Response.Write([String].Format("<error>Channel {0}</error>", ex.Message))
Response.End()
End Try
Try
Dim ds As New DataSet
data.FillArticles_GetListForChannel(ds, ArticleCount, ChannelID)
Dim dv As New DataView
dv.Table = ds.Tables(0)
dv.Sort = "DatePublished DESC"
rptRSS.DataSource = dv
rptRSS.DataBind()
Catch ex As Exception
Response.Clear()
Response.Write([String].Format("<error>Articles {0}</error>",
ex.Message))
Response.End()
End Try
data = Nothing

End Sub
Protected Function FormatForXML(ByVal input As Object) As String

Dim s As String = input.ToString() '; // cast the input to a string

'// replace those characters disallowed in XML documents
s = s.Replace("&", "&amp;")
s = s.Replace("""", "&quot;")
s = s.Replace("'", "&apos;")
s = s.Replace("<", "&lt;")
s = s.Replace(">", "&gt;")

Return s
End Function
End Class
 
Jim Hughes said:
Here is a simple ASPX example, the NewsDAL implemenation is left as an
exercise for the reader :)
Oh my, I did it again! (posting vb instead of C#

Here is the C# version converted at
http://www.developerfusion.com/utilities/convertvbtocsharp.aspx
(untested)

<!-- RSS.ASPX -->
<%@ Page language="vb" ContentType="text/xml" Codebehind="rss.aspx.cs"
AutoEventWireup="false" Inherits="news.rss" EnableSessionState="False"
enableViewState="False" buffer="True"%>
<asp:Repeater id="rptRSS" runat="server">
<HeaderTemplate>
<rss version="2.0">
<channel>
<title><%=ChannelTitle%></title>
<link><%=ChannelLink%></link>
<description>
<%=ChannelDescription%>
</description>
</HeaderTemplate>

<ItemTemplate>
<item>
<title><%# FormatForXML(DataBinder.Eval(Container.DataItem, "Title"))
%></title>
<description><%# FormatForXML(DataBinder.Eval(Container.DataItem,
"Description")) %></description>
<link>http://<%=Request.ServerVariables("SERVER_NAME") &
Request.ApplicationPath %>/Story.aspx?ID=<%#
DataBinder.Eval(Container.DataItem, "ArticleID") %></link>
<pubDate><%# String.Format("{0:R}", DataBinder.Eval(Container.DataItem,
"DatePublished")) %></pubDate>
</item>
</ItemTemplate>

<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>



// rss.aspx.cs
using System.Xml;
public class rss : System.Web.UI.Page
{
public string ChannelTitle = "";
public string ChannelLink = "";
public string ChannelDescription = "";

[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
}
private object designerPlaceholderDeclaration;

private void Page_Init(object sender, System.EventArgs e)
{
InitializeComponent();
}
protected System.Web.UI.WebControls.Repeater rptRSS;

private void Page_Load(object sender, System.EventArgs e)
{
int ArticleCount;
int ChannelID;
NewsDAL.Data data = new NewsDAL.Data(Global.ConnString);
try {
try {
ArticleCount = int.Parse(Request.QueryString("Count"));
} catch (void ) {
ArticleCount = 5;
}
try {
ChannelID = int.Parse(Request.QueryString("ChannelID"));
} catch (void ) {
ChannelID = 1;
}
DataSet ds = new DataSet();
data.FillRSSChannels_Get(ds, ChannelID);
DataRow dr = ds.Tables(0).Rows(0);
ChannelTitle = FormatForXML(dr("Title").ToString());
ChannelLink = FormatForXML(dr("Link").ToString());
ChannelDescription = FormatForXML(dr("Description").ToString());
} catch (Exception ex) {
Response.Clear();
Response.Write(String.Format("<error>Channel {0}</error>",
ex.Message));
Response.End();
}
try {
DataSet ds = new DataSet();
data.FillArticles_GetListForChannel(ds, ArticleCount, ChannelID);
DataView dv = new DataView();
dv.Table = ds.Tables(0);
dv.Sort = "DatePublished DESC";
rptRSS.DataSource = dv;
rptRSS.DataBind();
} catch (Exception ex) {
Response.Clear();
Response.Write(String.Format("<error>Articles {0}</error>",
ex.Message));
Response.End();
}
data = null;
}

protected string FormatForXML(object input)
{
string s = input.ToString();
s = s.Replace("&", "&amp;");
s = s.Replace(""", "&quot;");
s = s.Replace("'", "&apos;");
s = s.Replace("<", "&lt;");
s = s.Replace(">", "&gt;");
return s;
}
}
 
Rss can be a fairly rich format, or it can be insanely simple. Below,
Jim has done a great job of using existing page controls to generate
his own RSS feed without laying a finger on the XML classes.

To expand the example, you can do more than just run inline repeater
items. You can load user controls that render each item if you have
custom RSS elements that need to be displayed in each item.

You can also load user controls for the <description> element which
has the extreme benefit of allowing you to bind multiple types of
content to the same RSS feed and render each of them differently.

A second feature of this binding will be allowing your existing view
controls to be used in the RSS feed to generate the content or
<description> area. That means if you have a rich view already
displayed elsewhere in your site, you can reuse that logic so that
your RSS feeds look really awesome.
---

The easiest manner of generating RSS is still to use an XmlWriter to
spit out XML directly. It gives you a natural feel for every token in
the stream, and it happens to be one of the fastest methods for
creating XML compliant feeds.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Jim Hughes said:
Jim Hughes said:
Here is a simple ASPX example, the NewsDAL implemenation is left as an
exercise for the reader :)
Oh my, I did it again! (posting vb instead of C#

Here is the C# version converted at
http://www.developerfusion.com/utilities/convertvbtocsharp.aspx
(untested)

<!-- RSS.ASPX -->
<%@ Page language="vb" ContentType="text/xml" Codebehind="rss.aspx.cs"
AutoEventWireup="false" Inherits="news.rss" EnableSessionState="False"
enableViewState="False" buffer="True"%>
<asp:Repeater id="rptRSS" runat="server">
<HeaderTemplate>
<rss version="2.0">
<channel>
<title><%=ChannelTitle%></title>
<link><%=ChannelLink%></link>
<description>
<%=ChannelDescription%>
</description>
</HeaderTemplate>

<ItemTemplate>
<item>
<title><%# FormatForXML(DataBinder.Eval(Container.DataItem, "Title"))
%></title>
<description><%# FormatForXML(DataBinder.Eval(Container.DataItem,
"Description")) %></description>
<link>http://<%=Request.ServerVariables("SERVER_NAME") &
Request.ApplicationPath %>/Story.aspx?ID=<%#
DataBinder.Eval(Container.DataItem, "ArticleID") %></link>
<pubDate><%# String.Format("{0:R}", DataBinder.Eval(Container.DataItem,
"DatePublished")) %></pubDate>
</item>
</ItemTemplate>

<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>



// rss.aspx.cs
using System.Xml;
public class rss : System.Web.UI.Page
{
public string ChannelTitle = "";
public string ChannelLink = "";
public string ChannelDescription = "";

[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
}
private object designerPlaceholderDeclaration;

private void Page_Init(object sender, System.EventArgs e)
{
InitializeComponent();
}
protected System.Web.UI.WebControls.Repeater rptRSS;

private void Page_Load(object sender, System.EventArgs e)
{
int ArticleCount;
int ChannelID;
NewsDAL.Data data = new NewsDAL.Data(Global.ConnString);
try {
try {
ArticleCount = int.Parse(Request.QueryString("Count"));
} catch (void ) {
ArticleCount = 5;
}
try {
ChannelID = int.Parse(Request.QueryString("ChannelID"));
} catch (void ) {
ChannelID = 1;
}
DataSet ds = new DataSet();
data.FillRSSChannels_Get(ds, ChannelID);
DataRow dr = ds.Tables(0).Rows(0);
ChannelTitle = FormatForXML(dr("Title").ToString());
ChannelLink = FormatForXML(dr("Link").ToString());
ChannelDescription = FormatForXML(dr("Description").ToString());
} catch (Exception ex) {
Response.Clear();
Response.Write(String.Format("<error>Channel {0}</error>", ex.Message));
Response.End();
}
try {
DataSet ds = new DataSet();
data.FillArticles_GetListForChannel(ds, ArticleCount, ChannelID);
DataView dv = new DataView();
dv.Table = ds.Tables(0);
dv.Sort = "DatePublished DESC";
rptRSS.DataSource = dv;
rptRSS.DataBind();
} catch (Exception ex) {
Response.Clear();
Response.Write(String.Format("<error>Articles {0}</error>", ex.Message));
Response.End();
}
data = null;
}

protected string FormatForXML(object input)
{
string s = input.ToString();
s = s.Replace("&", "&amp;");
s = s.Replace(""", "&quot;");
s = s.Replace("'", "&apos;");
s = s.Replace("<", "&lt;");
s = s.Replace(">", "&gt;");
return s;
}
}
 
Back
Top