How to Create SiteMap dynamically in asp.net?
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 and 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.
Introduction :
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
User-agent: *
Sitemap: http://www.YOUR-DOMAIN-NAME.com/sitemap.aspx
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 :Code :
using System.Xml;
using System.Text;
using System.Data.SqlClient;
public partial class sitemap : System.Web.UI.Page
{
SqlConnection cn;
protected void Page_Load(object sender, EventArgs e)
{
try
{
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.WriteStartDocument();
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteStartElement("url");
writer.WriteElementString("loc", "Your Domain Name");
writer.WriteElementString("priority", "0.5");
writer.WriteEndElement();
//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
writer.WriteStartElement("url");
writer.WriteElementString("loc", mydomain + "/" + dtr["product_name"].ToString());
writer.WriteElementString("priority", "0.5");
//End URL
writer.WriteEndElement();
i++;
}
}
writer.WriteEndDocument();
writer.Close();
}
catch (Exception ex)
{
}
}
}
Now you can run the code and see the result.
Thank You.
Reference: http://dotnetsquare.com/resources/42-creating-dynamic-Asp-Net-sitemap