Turning a Data Page into RSS - LINQ Data Source

2012-03-18 by

RSS allows you connect to your web users in a way different than the web browser.  Here is a sample to expose your datasource in ASP.NET as an RSS feed.  The main parameter that tells the page how to display it’s data is the rss request parameter.  If the value is set to “1” then the page will display an RSS feed.  The code used here actually services all pages and categories under coding tips on the Pinecrest Consulting Website.

 

Step 1:

Add code to the Page_Load function to create an event handler when the data source is loaded.  This will allow you to determine output based on request parameters.

 

protected void Page_Load(object sender, EventArgs e)

    {

        dsPublicWebLinq.Selected += new EventHandler (dataSource_Selected);

    }

 

Step 2:

Add Code to the event handler to construct the RSS feed to the browser instead of the webpage that you have setup.  Replace any # Tags with the values or variables you need to customize the XML.

 

void dataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)

    {

 

if (Request["rss"] != null)

        {

            if (Request["rss"].ToString() == "1")

            {

                List items = e.Result as List;

                Response.Clear();

                Response.ContentType = "text/xml";

                System.Xml.XmlTextWriter xtwFeed = new System.Xml.XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);

                xtwFeed.WriteStartDocument();

                xtwFeed.WriteStartElement("rss");

                xtwFeed.WriteAttributeString("version", "2.0");

 

                xtwFeed.WriteStartElement("channel");

                xtwFeed.WriteElementString("title", this.Title);

                xtwFeed.WriteElementString("link", Request.Url.AbsoluteUri.ToLower().Replace("?rss=1","").Replace("&rss=1",""));

                xtwFeed.WriteElementString("description", this.MetaDescription);

                xtwFeed.WriteElementString("copyright", "Copyright " + DateTime.Now.Year + " #COMPANY NAME#");

                xtwFeed.WriteElementString("managingEditor", "#Email Address");

                xtwFeed.WriteElementString("ttl", "120");

 

                xtwFeed.WriteStartElement("image");

                xtwFeed.WriteElementString("url", "#RSS Image URL#");

                xtwFeed.WriteElementString("title", "#COMPANY NAME#");

                xtwFeed.WriteElementString("link", "#WEBSITE URL#");

                xtwFeed.WriteEndElement();

 

                foreach (item pst in items)

                {

                    xtwFeed.WriteStartElement("item");

                    xtwFeed.WriteElementString("title", pst.strTitle);

                    xtwFeed.WriteElementString("link", Page.ResolveUrl("~/PostDetails.aspx?id=" + pst.guidID.ToString()));

                    xtwFeed.WriteElementString("description", Server.HtmlDecode(pst.strShortDescription));

                    xtwFeed.WriteElementString("guid", pst.guidID.ToString());

                    xtwFeed.WriteElementString("pubDate", SharedFunctions.RFC822DateTime(pst.ldtDateCreated));

                    xtwFeed.WriteEndElement();

                }

 

                xtwFeed.WriteEndElement();

 

                xtwFeed.WriteEndDocument();

                xtwFeed.Flush();

                Response.OutputStream.Flush();

                Response.Flush();

                Response.End();

            }

        }

}

 

Step 3:

Add a link to your webpage to allow users to get to the RSS Feed.  Make sure you append the ?rss=1 or &rss=1 onto the end of the page’s url.

 

Following these steps allows you to reach more users using RSS.