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("&", "&")
s = s.Replace("""", """)
s = s.Replace("'", "'")
s = s.Replace("<", "<")
s = s.Replace(">", ">")
Return s
End Function
End Class