| RSS is quickly
becoming the content syndicator's XML Schema of choice. People are syndicating
feeds of their site content, their Blogs, and even their pet's birthdays
these days, it seems. We have one here: www.eggheadcafe.com/rss.xml in
RSS 2.0 format, and you're welcome to use it. We update it typically
once per week, and it contains the 5 most recent items from each of our
site's categories (including the latest forum posts and Hotlinks).
There are also plenty of RSS clients out there that
you can play with - Winforms versions, ASP.NET versions, and even server
controls. While putting together my own ASP.NET ServerControl which uses
a unique idea - passing the Control property settings through to an XSL
Stylesheet as XSL parameters, so all the heavy lifting is actually done
via the XSL which is embedded as a compiled resource right into the control
assembly - - I realized that a lot of people might really be more interested
in a very easy - - I mean really easy way to handle
an RSS feed and make a nice display out of it in an ASP.NET Web page.
After a little thought, I realized that our good friends, the DataSet
and the DataGrid provide such a choice. All we need to do is use the
DataSet.ReadXml() method and point it to the http URI of the rss feed
of our choice, find which table contains the nodelist of <link> elements,
set this as the DataSource of the DataGrid, call DataGrid.DataBind(),
and man -- we are DONE!
The only other thing we need to do is set the display
properties with the Property Builder sheets, and put in some code to
handle paging if we want it, and we've got ourselves a very professional
- looking RSS feed display!
Not only that, but if you wanted to , we could populate
a listbox with a whole list of different feed URLS and names, let the
user choose which feed they want to look at, and Voila, mes amis! Instant
web Nirvana (sort of...).
The basic code to do this is as follows:
private void Page_Load(object sender, System.EventArgs
e)
{
DataSet ds = new DataSet();
ds.ReadXml("http://www.eggheadcafe.com/rss.xml",XmlReadMode.Auto);
DataGrid1.DataSource=ds.Tables[2];
DataGrid1.DataBind();
}
Is that simple enough? I would
hope so! Your in-page DataGrid HTML Code after setting some nice properties
in the Designer, might look like this:
<asp:DataGrid id="DataGrid1" style="Z-INDEX:
101; LEFT: 24px; POSITION: absolute; TOP: 16px" runat="server"
Width="920px" AutoGenerateColumns="False" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4" Font-Names="Arial" GridLines="Horizontal" Font-Size="Smaller"
AllowPaging="True" PageSize="5" Height="168px">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<AlternatingItemStyle Wrap="False"></AlternatingItemStyle>
<ItemStyle Wrap="False" ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="Date" HeaderText="Date"></asp:BoundColumn>
<asp:BoundColumn DataField="Title" HeaderText="Subject"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="link" DataTextField="description" HeaderText="Link"></asp:HyperLinkColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
For handling paging,
I didn't even bother to cache the DataSet or DataTable here because
the RSS.XML source is pretty small, so this will do it:
private void DataGrid1_PageIndexChanged(object
source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataSet ds = new DataSet();
ds.ReadXml("http://www.eggheadcafe.com/rss.xml",XmlReadMode.Auto);
DataGrid1.DataSource=ds.Tables[2];
DataGrid1.DataBind();
}
Finally, if you are behind a firewall
such as ISA Server at work, this code is not "your browser" and so
you'll need to add some credentials to the GlobalProxySettings with
the following code (which I've included in the download as well as
in the web.config):
string proxyIp=ConfigurationSettings.AppSettings["proxyIp"].ToString();
string proxyPort=String.Empty;
string proxyUserName = String.Empty;
string proxyPassword = String.Empty;
string proxyDomain = String.Empty;
if(proxyIp.Length >0)
{
proxyPort = ConfigurationSettings.AppSettings["proxyPort"].ToString();
proxyUserName = ConfigurationSettings.AppSettings["proxyUserName"].ToString();
proxyPassword =ConfigurationSettings.AppSettings["proxyPassword"].ToString();
proxyDomain = ConfigurationSettings.AppSettings["proxyDomain"].ToString();
NetworkCredential cred =new NetworkCredential(proxyUserName,proxyPassword,proxyDomain);
WebProxy proxyObject =new WebProxy(proxyIp+":" + proxyPort,true,new
string[0],cred);
GlobalProxySelection.Select = proxyObject;
}
Download a working Solution in
VS.NET 2003 format below.
Download the code accompanying this article
| Peter Bromberg is a C# MVP, MCP, and .NET consultant who has worked in the banking and financial industry for 20 years. He has architected and developed web - based corporate distributed application solutions since 1995, and focuses exclusively on the .NET Platform. Pete's samples at GotDotNet.com have been downloaded over 41,000 times. You can read Peter's UnBlog Here. --><-- NOTE: Post QUESTIONS on FORUMS! |  | Do you have a question or comment about this article? Have a programming problem you need to solve? Post it at eggheadcafe.com forums and receive immediate email notification of responses.
|