LINQ makes everyday tasks a joy and not a chore. This simple code example was implemented and deployed on http://www.causewaydesign.co.uk/ in 15 mins.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public partial class rss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataClassesDataContext dataContext = new DataClassesDataContext();
// Select data
var itemsQuery = from post in dataContext.tbl_causeway_news
orderby post.news_date descending
select new
{
post.news_id,
post.news_title,
post.news_text,
post.news_date,
};
// Prepare response
Response.Buffer = false;
Response.Clear();
Response.ContentType = "application/xml";
// Create an XmlWriter to write the feed into it
using (XmlWriter writer = XmlWriter.Create(Response.OutputStream))
{
// Set the feed properties
SyndicationFeed feed = new SyndicationFeed
("Stuart Manning",
"Causeway Design - Web Development, Belfast, Northern Ireland",
new Uri("http://www.causewaydesign.co.uk"));
// Add authors
feed.Authors.Add(new SyndicationPerson
("info@causewaydesign.co.uk",
"Stuart Manning",
"http://www.causewaydesign.co.uk"));
// Set copyright
feed.Copyright = new TextSyndicationContent
("© Copyright 2005-"+DateTime.Now.Year.ToString()+" Causeway Design");
// Set generator
feed.Generator = "Causeway Design RSS Generator";
// Set language
feed.Language = "en-GB";
// Add post items
List items = new List();
foreach (var Post in itemsQuery)
{
SyndicationItem item = new SyndicationItem();
item.Id = Post.news_id.ToString();
item.Title = TextSyndicationContent.CreatePlaintextContent(Post.news_title);
item.Content = SyndicationContent.CreateXhtmlContent(Post.news_text);
item.PublishDate = Post.news_date.Value.ToUniversalTime();
//item.Categories.Add(new SyndicationCategory(Post.Category));
items.Add(item);
}
feed.Items = items;
// Write the feed to output
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(writer);
writer.Flush();
}
Response.End();
}
}