RSS.NET Code Examples

Collection of C# and VB.NET examples using the RSS.NET class library

Print RSS.NET CSS 2.0 XHTML 1.1

Reading

To read an RSS feed, use the following syntax:

C#:
string url = "http://sourceforge.net/export/rss2_sfnews.php?feed";
RssFeed feed = RssFeed.Read(url);

VB.NET:
Dim url As String = "http://sourceforge.net/export/rss2_sfnews.php?feed"
Dim feed As RssFeed = RssFeed.Read(url)
Creates a new instance of the RssFeed class with the contents of the SourceForge RSS feed.

To bind a ListBox to the items of a channel:

C#:
RssChannel channel = (RssChannel)feed.Channels[0];
listBox.DataSource = channel.Items;

VB.NET:
Dim channel As RssChannel = feed.Channels(0)
listBox.DataSource = channel.Items
Sets a listBox's datasource property to the downloaded feed's first channel's item collection.

To display the item's description in a Label:

C#:
private void listBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
    RssItem item = (RssItem)listBox.SelectedItem;
    label.Text = item.Description;
}

VB.NET:
Private Sub listBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                         Handles ListBox.SelectedIndexChanged
    Dim item As RssItem = listBox.SelectedItem
    label.Text = item.Description
End Sub
Sets a label's text property to the listBox's selected item's description on the listBox's SelectedIndexChanged event.

Writing

C#:
oleDbDataAdapter.Fill(exampleData);

VB.NET:
OleDbDataAdapter.Fill(exampleData)
The oleDbDataAdapter uses a oleDbConnection to a JET database to fill a typed dataset.

For each row in the table, add an item to the channel:

C#:
RssChannel channel = new RssChannel();
foreach(ExampleData.NewsRow row in exampleData.News.Rows)
{
    RssItem item = new RssItem();
	 
    item.Title = row.news_title;
    item.Description = row.news_description;
    item.PubDate = row.news_date.ToUniversalTime();
	 
    channel.Items.Add(item);
}

VB.NET:
Dim row As ExampleData.NewsRow
Dim channel As New RssChannel
For Each row In exampleData.News.Rows
   Dim item As New RssItem

   item.Title = row.news_title
   item.Description = row.news_description
   item.PubDate = row.news_date.ToUniversalTime

   channel.Items.Add(item)
Next
For each row, a new RssItem is created, filled with data, and added to the item collection.

Set the channel properties:

C#:
channel.Title = "My channel's title";
channel.Description = "My channel's description";
channel.LastBuildDate = channel.Items.LatestPubDate;

VB.NET:
channel.Title = "My channel's title"
channel.Description = "My channel's description"
channel.LastBuildDate = channel.Items.LatestPubDate
Sets the channel's lastBuildDate the the most current item's pubDate.

Finally, create and output the feed:

C#:
RssFeed feed = new RssFeed();
feed.Channels.Add(channel);
Response.ContentType = "text/xml";
feed.Write(Response.OutputStream);
Response.End();

VB.NET:
Dim feed As New RssFeed
feed.Channels.Add(channel)
Response.ContentType = "text/xml"
feed.Write(Response.OutputStream)
Response.End()
Calls the feed's method to write the RSS feed to the http stream.

SourceForge.net Logo

RSS.NET Copyright © 2002-2005 ToolButton Inc.. All Rights Reserved.

RSS.NET (http://rss-net.sourceforge.net/)
Copyright © 2002-2005 ToolButton Inc.. All Rights Reserved.

RSS 2.0 (http://backend.userland.com/rss/)
Copyright © 1997-2002 UserLand Software. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

As required by UserLand Software for their documentation portions:
"This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and these paragraphs are included on all such copies and derivative works. This document may not be modified in any way, such as by removing the copyright notice or references to UserLand or other organizations. Further, while these copyright restrictions apply to the written RSS specification, no claim of ownership is made by UserLand to the format it describes. Any party may, for commercial or non-commercial purposes, implement this format without royalty or license fee to UserLand. The limited permissions granted herein are perpetual and will not be revoked by UserLand or its successors or assigns. This document and the information contained herein is provided on an "AS IS" basis and USERLAND DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.