Generate Google Site Map in VB.NET

Google Site Maps are used by Google to index your website. You can create a site map of your website and submit to Google Site Maps for telling Google about all your web pages. Here is the URL for Google Site Maps

I use a simple technique to create the site maps of all my site.

First of create a new .ASPX page in Visual Studio and name it GoogleMap.aspx

Now go to the code file for this and add the following code. ( You would need to tweak the information for your own website)

Imports System.IO
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Text
Partial Class GoogleMap
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
‘Put user code to initialize the page here
Dim cn As New SqlConnection
Dim cl As New clsMain
cl.OpenConnection(cn) ‘ This is my function to open a connection ( use your own code )
Response.ContentType = “text/xml”
Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
objX.WriteAttributeString(“xmlns”, “”)

‘Handles database connection string and command

‘ Index Page

objX.WriteElementString(“loc”, “”)
objX.WriteElementString(“lastmod”, Format(Now, “yyyy-MM-dd”))
objX.WriteElementString(“priority”, “1.0”)

‘ New Articles Page

‘ ‘ objX.WriteElementString(“loc”, objReader(“Name”))
objX.WriteElementString(“loc”, “”)
objX.WriteElementString(“lastmod”, Format(Now, “yyyy-MM-dd”))
objX.WriteElementString(“priority”, “1.0”)

‘Now generate the url for each articles. The Articles are stored in tblArticles and each Article has a Article ID
Dim strSQL As String

strSQL = “Select ArticleID , ArticleDate from tblArticles”

Dim objCommand = New SqlCommand(strSQL, cn)
Dim objReader As SqlDataReader
objReader = objCommand.ExecuteReader()

‘Populate record for the RSS feed
While objReader.Read()
objX.WriteElementString(“loc”, “ objReader(“ArticleID”) & “.aspx” ‘ For each Article ID a new page name is created
objX.WriteElementString(“lastmod”, Format(Now(), “yyyy-MM-dd”))
objX.WriteElementString(“priority”, “1.0”)
End While



‘ objX.WriteEndElement()

End Sub
End Class

If you browse the page then you would see a XML output of all your pages. This page can be submitted to Google Site Maps and Google will index all your submitted pages.

This is a dynamic way of creating the site map and it will ensure that your site map is always updated without any intervention. When you create a new Article then the Google Site Map is always updated.

Now one more thing at the end . You can use the Cache in your page so that each time your Site Map is accessed your page does not need to open a connection and create a list of all your pages on the fly.

In the Google.aspx page add a line

<%@ OutputCache Duration="60000" VaryByParam="none" %>

as shown below in the following example


<%@ OutputCache Duration="60000" VaryByParam="none" %>

<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”GoogleMap.aspx.vb” Inherits=”GoogleMap” %>