How to Create SiteMap Automatically in

In this article I'm going to show you how to create sitemap automatically. This sitemap is used to create SEO friendly page after fetching the data from database. This Sitemap help for crawling the pages in search engine for that you need to create this Sitemap and map to those search engines.

How to Create SiteMap Automatically in

The sitemap is a great way to tell the search engines about all your web pages. For that first we need to create robots.txt file in your project main folder

Now add one web page to the project and change the names of the page Default.aspx to sitemap.aspx and after adding page then delete all html code in this page

Replace this html code from deleted html code in sitemap.aspx

 <%@ Page Language="C#" AutoEventWireup="true" 
 CodeFile="sitemap.aspx.cs" Inherits="sitemap" 
  ContentType ="text/xml" %>

Because this is an XML sitemap, we want to send the XML directly to the browser. So, even though the page is an .aspx extension, when its sent to the browser we will make sure it thinks its XML by adding ContentType ="text/xml" in the page directive.

Now goto the .aspx.cs page and add the below code :

C# Coding : Namespace

The Namespace which i'm using in my project are

 using System.XML;
 using System.Text;
 using System.Data;
 using System.Data.SqlClient;

C# Coding : Page Load

 protected void Page_Load(object sender, EventArgs e)
                SqlConnection cn = new SqlConnection("Data Source=.; Database=Databasename; User ID=; Password=;");
                string mydomain = ""; //ex :

                string strSql = "select post_id, post_title from webpost";
                SqlDataAdapter dacontent = new SqlDataAdapter(strSql, cn);
                DataSet dscontent = new DataSet();
                dacontent.Fill(dscontent, "SiteMap"); //here we fill all the products into dataset

                //Now we are going to create XML file using XMLTextWriter

                XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
                writer.WriteStartElement("urlset", "");


                writer.WriteElementString("loc", "");
                writer.WriteElementString("priority", "0.5");

                //Now we loop for creating the XML node for all products
                if (dscontent.Tables[0].Rows.Count > 0)
                    DataRow dtr;
                    int i = 0;
                    while (i < dscontent.Tables[0].Rows.Count)
                        dtr = dscontent.Tables[0].Rows[i];

                        //Creating the  element

                        writer.WriteElementString("loc", mydomain + "/" + dtr["post_title "].ToString());
                        writer.WriteElementString("priority", "0.5");

                        //End URL


            catch (Exception ex)



Post a Comment