Thursday, September 10, 2009

Google SiteMap File

Generating a Google SiteMap File


Google provides a free service, named Google SiteMaps, that you can use to improve the way that Google indexes the pages on your website.

For example, you can use Google SiteMaps to discover which Google search queries have returned pages from your website and the ranking of your pages in Google search results. You also can use Google SiteMaps to view any problems that the Google crawler encounters when indexing your site.


You can sign up for Google SiteMaps by visiting the following URL:


To use Google SiteMaps, You Must Provide Google With The Url Of A Google Sitemap File Hosted On Your Website.

The Google SiteMap file is an XML file that contains a list of URLs you want Google to index.

The Google SiteMap XML file has the following format:


Google Sitemap File
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="">



The Google SiteMap file contains a simple list of <url> elements that contain <loc> elements representing the location of the URL and <lastmod> elements representing the last modified date of the URL.


The Google SiteMap file also can contain <changefreq> and <priority> elements. The <changefreq> element indicates how frequently a URL changes, and the <priority> element represents the priority of a URL relative to other URLs in your site. These elements are optional and are ignored here.


When you sign up at the Google SiteMaps website, submit the URL of the above file when you are asked to enter your SiteMap URL. The Google service retrieves your SiteMap from the handler automatically.


How to generate the ‘Google SiteMap XML file’ file automatically -


You can generate a Google SiteMap file automatically from an ASP.NET SiteMap. The below HTTP Handler generates a Google SiteMap File that conforms to Google's requirements for a valid SiteMap file.


HTTP Handler
<%@ WebHandler Language="VB" Class="PublicSiteMap" %>
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.IO
Public Class PublicSiteMap
    Implements IHttpHandler
    Private _xmlWriter As XmlWriter
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler
        context.Response.ContentType = "text/xml"
        Dim settings As New XmlWriterSettings()
        settings.Encoding = Encoding.UTF8
        settings.Indent = True
        _xmlWriter = XmlWriter.Create(context.Response.OutputStream, settings)
        _xmlWriter.WriteStartElement("urlset", "")
        ' Add root node
        ' Add all other nodes
        Dim nodes As SiteMapNodeCollection = SiteMap.RootNode.GetAllNodes()
        For Each node As SiteMapNode In nodes
    End Sub
    Private  Sub AddUrl(ByVal node As SiteMapNode)
        ' Skip empty Urls
        If String.IsNullOrEmpty(node.Url) Then
        End If
        ' Skip remote nodes
        If node.Url.StartsWith("http",True,Nothing) Then
        End If
        ' Open url tag
        ' Write location
        ' Write last modified
        ' Close url tag
    End Sub
    Private Function GetFullUrl(ByVal url As String) As String
        Dim context As HttpContext =  HttpContext.Current
        Dim server As String =  context.Request.Url.GetComponents( UriComponents
        Return Combine(server,url)
    End Function
    Private Function Combine(ByVal baseUrl As String, ByVal url As String) As String
        baseUrl = baseUrl.TrimEnd(New Char() {"/"c})
        url = url.TrimStart(New Char() {"/"c})
        Return baseUrl + "/" + url
    End Function
    Private Function GetLastModified(ByVal url As String) As String
        Dim context As HttpContext =  HttpContext.Current
        Dim physicalPath As String =  context.Server.MapPath(url)
        Return File.GetLastWriteTimeUtc(physicalPath).ToString("s")
    End Function
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Return True
        End Get
    End Property
 End Class



Hope this helps.


Thanks & Regards,

Arun Manglick || Senior Tech Lead



No comments:

Post a Comment