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:
|
<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
01
02
03
04
05
06
07
08
09
10
11
12
13
|
<? xml version = "1.0" encoding = "UTF-8" ?> < url > < lastmod >2012-01-01</ lastmod > < changefreq >monthly</ changefreq > < priority >0.8</ priority > </ url > < url > < 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:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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" ); 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:
1
|
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SiteMap.aspx.cs" Inherits="SiteMap" %> |