This is an example of how to use the RssWriter class for generating a RSS 2.0 feed.

The XML output can be spooled to a stream, a writer, a string builder or a file.

 

RssFeed rss = new RssFeed
	{
		Channel = new RssChannel("YourCompany Channel", new Uri("http://www.YourCompany.com/"), "Channel Description")
		{
			PublishedDate = new DateTime(2012, 1, 1),
			LastBuildDate = DateTime.Now,
			Copyright = String.Format("Copyright YourCompany {0}", DateTime.Now.Year),
			Language = CultureInfo.CreateSpecificCulture("en-US"),
			ManagingEditor = "editor@YourCompany.com",
			WebMaster = "webmaster@YourCompany.com"
		}
	};

rss.Channel.AddCategory(new RssCategory("Category name", "Domain name"));
rss.Channel.AddCategory(new RssCategory("Category taxonomy/Level 2/Level 3"));

rss.AddItem(
	new RssItem("Item #00001", new RssItem.Guid("http://www.YourCompany.com/rss/#00001"))
	{
		ItemSource = new RssItem.Source("Source Title", new Uri("http://www.republishedfromhere.com"))
	}
);

rss.AddItem(
	new RssItem("Item #00002", new RssItem.Guid("http://www.YourCompany.com/rss/#00002"))
	{
		Author = "Author's name",
		Link = new Uri("http://www.linktothisnews.com")
	}
);

rss.AddItem(
	new RssItem("Item #00003", new RssItem.Guid("http://www.YourCompany.com/rss/#00003"))
	{
		Description = "Description of Item #00003",
		PublishedDate = DateTime.Now
	}
);

using (RssWriter writer = new RssWriter(rss))
{
	StringBuilder sb = new StringBuilder();
	writer.Write(sb);

	Console.WriteLine(sb.ToString());
}

 

 

There are several options for spooling the output XML; they are all overload of the Write() method of the RssWriter class:

  • Stream
  • File (by specifying the file full path and name)
  • StringBuilder
  • XmlWriter
  • TextWriter

 

For generating the XML in memory and assigning to a StringBuilder, for instance, it is as simple as writing this:

StringBuilder sb = new StringBuilder();

rss.Write(sb);

 

For spooling directly to a file it is even simpler:

rss.Write(@"C:\temp\myrss.xml");

 

This writes file “myrss.xml” into the temp folder on drive C: of the computer where the RssWriter is executed.

 

If you want to create an ASP.NET web form that generates the RSS feed and writes the output as response to an RSS aggregator, there are two steps to consider:

  1. Set the correct MIME type of the Response to application/rss+xml
  2. Ignore any HTML in the aspx page (you can actually remove the entire HTML content of the page), and writes the XML of the RSS feed directly to the Response output stream

 

This is an example that can be implemented in the code behind of the aspx page:

protected void Page_Load(object sender, EventArgs e)

{

       Response.ContentType = "application/rss+xml";

       GenerateRssFeed(Response.Output);

}

 

private void GenerateRssFeed(TextWriter output)

{

       using (RssWriter rss = new RssWriter())

       {

              ...

 

              rss.Write(output);

       }

}

Last edited Sep 26, 2012 at 3:45 PM by stefanotempesta, version 2