Create a SiteMap with ASP.NET

What are Sitemaps?

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.
Sitemap tag definitions

Tag Required? Description
<urlset> Required Encloses all information about the set of URLs included in the Sitemap.
<url> Required Encloses all information about a specific URL.
<loc> Required Specifies the URL. For images and video, specifies the landing page (aka play page, referrer page). Must be a unique URL.
<lastmod> Optional The date the URL was last modifed, in YYYY-MM-DDThh:mmTZD format (time value is optional).
<changefreq> Optional Provides a hint about how frequently the page is likely to change. Valid values are:

  • always. Use for pages that change every time they are accessed.
  • hourly
  • daily
  • weekly
  • monthly
  • yearly
  • never. Use this value for archived URLs.
<priority> Optional Describes the priority of a URL relative to all the other URLs on the site. This priority can range from 1.0 (extremely important) to 0.1 (not important at all).

Does not affect your site’s ranking in Google search results. Because this value is relative to other pages on your site, assigning a high priority (or specifying the same priority for all URLs) will not help your site’s search ranking. In addition, setting all pages to the same priority will have no effect.

Sitemap Example

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.ashishblog.com/</loc>
    <lastmod>2012-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>http://www.ashishblog.com/about-me/</loc>
    <priority>0.5</priority>
  </url>
</urlset>

First, We need to create table or store procedure to get url from sites.
Here I created on table named GoogleIndexProduct with two column : URl and UpdatedDate
Now I used Following code to generate SiteMap xml file from that created table.
SiteMap.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Xml;
using System.Text;
 
public partial class SiteMap: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            using (XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("urlset");
                writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
                
                string connect = WebConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(connect))
                    {
                        con.Open();
                        string query = "SELECT [URL],Date FROM [GoogleIndexProduct]";
                        SqlCommand cmd = new SqlCommand(query, con);
                        SqlDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            writer.WriteStartElement("url");
                            writer.WriteElementString("loc", dr[0].ToString());
                            writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", dr[1]));
                            writer.WriteElementString("changefreq", "weekly");
                            writer.WriteElementString("priority", "1.0");
                            writer.WriteEndElement();
                        }
                        con.Close();
                        con.Dispose();
                    }
             
                
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
            }
            Response.End();
           
        }
    }

SiteMap.aspx:
The first thing I did when adding the page to the site was to remove all but the top line of the aspx file. This prevents any html being added to the response and invalidating the xml of the sitemap. All that appears, therefore is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SiteMap.aspx.cs" Inherits="SiteMap" %>

I then logged into my Google account, and submitted the URL (http://www.ashishblog.com/SiteMap.aspx) for my site map, having tested that it validates against the protocol.