Computer Tips and Tricks

7/10/12

How to Create RSS Feed or XML Sitemap Dynamically in Asp.net

Please Leave your Valuable Comments if you like this Article!
                       sitemap rss feed asp.net dynamically
A Sitemap is very Important for any website because when we submit sitemap to search engines then they can crawl our website fast, there are many online tools available to create sitemap for websites but they work only for static websites and if you are an ASP.net developer then you should develop your own site map system which updates regularly for dynamic pages.
So I am creating XML RSS Feed which can work as a Sitemap as well.
First Create a Aspx Page, for example I am creating rss.aspx                                                                  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Text;

public partial class rss : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        DataTable dt = new DataTable();

        string query = "select * from articles";
//Binding data from Database to datatable you can use your own methods to binding data into datatable.
        dt = dc.get_datatable(query);

        //Start writer rss.xml is the file which will be genrated after when we run this page,
        XmlTextWriter TextWriter = new XmlTextWriter(Server.MapPath("~/rss.xml"), System.Text.Encoding.UTF8);
        //Start XM DOcument
        TextWriter.WriteStartDocument(true);
        TextWriter.Formatting = Formatting.Indented;
        TextWriter.Indentation = 2;

        Response.Clear();
        Response.ContentType = "text/xml";
              
        //Below tags are mandatory rss tag
        TextWriter.WriteStartElement("rss");
        TextWriter.WriteAttributeString("version", "2.0");

        // Channel tag will contain RSS feed details
        TextWriter.WriteStartElement("channel");
        TextWriter.WriteElementString("title", "Your website Title Will be Here");
        TextWriter.WriteElementString("link", "put your website url here with http://");
        TextWriter.WriteElementString("description", "Desciption of you website here.");
        TextWriter.WriteElementString("copyright", "Any Copyright Info you can write here");
        // Reading data from datatable which contains the values of our Dynamic URL

        foreach (DataRow oFeedItem in dt.Rows)
        {
            TextWriter.WriteStartElement("item");
            TextWriter.WriteElementString("title", oFeedItem["title"].ToString());
            TextWriter.WriteElementString("description", oFeedItem["title"].ToString());
        
  // Now you can see in link field I am giving the value dynamically loop will continue for the all Article ID like our dynamic url is http://example.com/articleid.aspx?=33. So it will add all dynamic IDs or article in our RSS feed.

 TextWriter.WriteElementString("link", "http://example.com/articleid.aspx?="+oFeedItem["I_Articleid"].ToString());
            TextWriter.WriteEndElement();
        }
        TextWriter.WriteEndElement();
        TextWriter.WriteEndElement();
        TextWriter.WriteEndDocument();
        TextWriter.Flush();
        TextWriter.Close();
        Response.End();
        Response.Redirect("rss.xml");
    }
}

I have Code whole coding on page load which means whenever rss.aspx loads our rss.xml sitemap will be updated. If you this to be done automatically then you can call rss.aspx page after any changes in website, or you can do it manually after any updates or daily,this RSS Feed or Sitemap is tested with Google and works Perfectly for my website, and RSS feed tested with Feedburner.

Please Read the Comments on Code Carefully to Implement RSS Feed and XML Sitemap in your website, if you need any kind of help Regarding this Code you can Contact me.

Please Leave Comments your Comments are always Helpful.


comments powered by Disqus