|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe code attached to this article handles the creation of a web feed; either in the format of RSS 2.0 or Atom syndication. In this article, I will explain how to use the code. BackgroundA web feed is a syndication of content, that allows internet users to subscribe to the content and receive notification about new additions, or easily retrieve and remix the content which is provided in a common format. A web feed can include files, e.g., video, sound, images, or PDF documents. Some feed readers/aggregators are able to handle the playback of sound and video. In iTunes, it is possible to subscribe to podcasts and retrieve new episodes automatically, e.g., new radio programs or TV shows in a series. I set out to create a common class to handle the syndication of content. Along the way, I learned more about the standards RSS and Atom, and the requirements to make iTunes accept and successfully subscribe to a web feed. I tested the resulting feeds in an online validation tool here. Using the CodeThe code is written in C#, and consists of the four classes
The The These classes can be used in an ASP.NET website as described in the following section. Using Visual Studio, a website can be created by choosing File / New / Web Site. Alternatively, the demo website in the attached demo package contains it all. Preparing the website
Using the feed classes
Feed feed = new Feed("Northwind Employees", "Employees in the Northwind database");
string connectionString =
@"Data Source=.\SqlExpress;Initial Catalog=Northwind;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(@"
SELECT LastName, FirstName, Title, TitleOfCourtesy, HireDate, Notes
FROM Employees
ORDER BY HireDate DESC, LastName ASC"
, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// TODO: CREATE FEED ITEM (STEPS 3-5 BELOW)
// TODO: ADD FEED ITEM TO FEED (STEP 6 BELOW)
}
}
connection.Close();
}
}
Inside the inner loop above, I create and add each feed item, see steps 3-6 below. FeedItem feedItem = new FeedItem(title)
{
Description = (string)reader["Notes"],
PublishDate = (DateTime)reader["HireDate"]
};
string urlToFile = "http://nothing.nowhere/avatar.jpg";
string mimeType = "image/jpeg";
int sizeInBytes = 30000;
feedItem.Enclosure = new FeedItemEnclosure(urlToFile, mimeType, sizeInBytes);
feed.Items.Add(feedItem);
feed.WriteAtom(this.Context)
The generated Atom feed has the following XML content: <?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="html">Northwind Employees</title>
<link rel="self" type="application/atom+xml"
href="http://localhost:51529/FeedDemoWebSite/default.aspx" />
<subtitle type="html">Employees in the Northwind database</subtitle>
<id>http://localhost:51529/FeedDemoWebSite/default.aspx</id>
<updated>2008-10-26T16:40:42.5191442Z</updated>
<entry>
<title>Ms. Anne Dodsworth, Sales Representative</title>
<updated>1994-11-15T00:00:00Z</updated>
<content type="html">
Anne has a BA degree in English from St. Lawrence College.
She is fluent in French and German.
</content>
<link rel="enclosure" type="image/jpeg"
length="30000" href="http://nothing.nowhere/avatar.jpg" />
</entry>
<entry>
...
</entry>
</feed>
The code in the classesThe source code is commented, and I hope that the comments will provide some answers to questions that may arise there. Therefore, I chose not to go into details with the classes here. If you look at the code in the classes, I will be happy to hear about errors, improvements, suggestions, or other comments. I included some Points of InterestNote that to create a podcast that users can subscribe to from iTunes, enclose a file that iTunes supports, and provide the correct MIME type, e.g., "video/mp4" or "audio/mp3". For a list of MIME types for iTunes, see the section "iTunes RSS Tags" on Apple's page about iTunes podcasts. History
|
||||||||||||||||||||||||||||||||||||||||