DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Rendering RSS feeds in .NET

Denzel D. user avatar by
Denzel D.
·
Jun. 05, 10 · · Interview
Like (0)
Save
Tweet
6.18K Views

Join the DZone community and get the full member experience.

Join For Free

I covered the process of reading RSS feeds in my previous article , but what if you want to create your own RSS feeds? Instead of directly working with XML creation, System.ServiceModel.Syndication can help the developers by providing some classes that organize the feed rendering process.

To show how to do this, I am creating a project that will get the feed data from a database and based on that, will render the feed. I created a simple SQL database with one table called posts that contains two columns – title and contents. Pretty self-descriptive, and should make the references in the feed clearer.

First of all, I need to get the needed feed data from the database. To do this, I am using the code below (you need to add a reference to the System.Data.SqlClient namespace) that basically executes a SQL query and retrieves the data from all columns, later returning a dictionary:public Dictionary<string, string> GetValues()

{
Dictionary<string, string> values = new Dictionary<string, string>();

using (SqlConnection connection = new SqlConnection(@"Data Source=DENNIS-PC\SQLEXPRESS;Initial Catalog=blogposts;User Id=dennis;Password=dennis;"))
{
connection.Open();
string query = "SELECT * FROM posts";
SqlCommand command = new SqlCommand(query, connection);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
values.Add(reader.GetValue(0).ToString().Trim(), reader.GetValue(1).ToString().Trim());
}
}
}
}
return values;
}

Considering that the data is stored in a database, the process can be easily adapted to a real-world solution. The returned dictionary in this case will contain the post title as the key and the contents as the value. Now to the actual feed creation.

Same as with feed reading, feed creating is handled via SyndicationFeed. The returned result is passed to a Rss20FeedFormatter, that serializes the feed to RSS 2.0 format. Here is what the code looks like:

public Rss20FeedFormatter GetFeed(Dictionary<string,string> items)
{
SyndicationFeed feed = new SyndicationFeed("Feed Title", "Description", new Uri("http://myfeedurl"));

feed.Authors.Add(new SyndicationPerson("authoremail@domain", "Dennis", "http://userurl"));
feed.Categories.Add(new SyndicationCategory("Feed Category"));
feed.Description = new TextSyndicationContent("This is the feed description");

List<SyndicationItem> itemList = new List<SyndicationItem>();

foreach (string key in items.Keys)
{
SyndicationItem item = new SyndicationItem(key, items[key],new Uri("http://alternateurl"));
itemList.Add(item);
}

feed.Items = itemList;

return new Rss20FeedFormatter(feed);
}

 

As you can see here, I am creating a new instance of SyndicationFeed that sets the feed title, description and URL. I am using dummy URLs for testing purposes only, so you can change those accordingly. I am also setting the feed author, description and category. This helps later on when the user wants to identify your feed.

A feed is composed of a multitude of SyndicationItem instances; therefore I am passing the dictionary I created above as the parameter to the function and passing through each single entry to create a new syndication item. I am also adding every new item to a list that is later on assigned to the feed.Items property.

To test all the above code, you can use the following snippet:

Dictionary<string, string> values = GetValues();
Rss20FeedFormatter formatter = GetFeed(values);

foreach (SyndicationItem item in formatter.Feed.Items)
{
Debug.Print(item.Title.Text);
}

 

Once you get it up and running, you will see the item titles shown in the Output window in Visual Studio.

Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Testing Under the Hood Or Behind the Wheel
  • After COVID, Developers Really Are the New Kingmakers
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • Debugging the Java Message Service (JMS) API Using Lightrun

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo