The sitemap is a great way to tell the search engines about all your web pages. If you develop a website which has many dynamic pages and you want to get crawled/found these pages in search engines.
In this article I am going explain how to create sitemap dynamically. Suppose I have created one site for shopping site and which has many products stored into database. It automatically creates SEO friendly page after fetching the data from database. To get these pages crawled by search engines you have to create one site map and link that site map to search engine.
Firstly you want to make sure you have a robots.txt file in the root of you're website, and add the following to it
Obviously if you already have a robots.txt then just add the bolded line and change the domain name to your website domain.
Now the query is to create the sitemap.aspx file.
Now add one new aspx page to your website as sitemap.aspx, Delete all the code in the aspx page and paste the below code in the aspx page :
<%@ 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 :
public partial class sitemap : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
cn = new SqlConnection("Your connection string");
string mydomain = "your domain name"; //ex : http://www.example.com
string strSql = "select product_name from product_master";
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.WriteElementString("loc", "Your Domain Name");
//Now we loop for creating the XML node for all products
if (dscontent.Tables.Rows.Count > 0)
int i = 0;
while (i < dscontent.Tables.Rows.Count)
dtr = dscontent.Tables.Rows[i];
writer.WriteElementString("loc", mydomain + "/" + dtr["product_name"].ToString());
catch (Exception ex)
Now you can run the code and see the result.