• http://www.techneurons.com/career/
  • experienced programming consultants for hire !!!

    Contact Now

    Visual Basic .NET Articles

    KB: Generate Google Sitemap for ASP.NET website using VB.NET

    SATURDAY, APRIL 17, 2010

    A sitemap is a list of pages available on a website. This was used to help visitors to provide an overall navigation of the website, but now sitemaps are mainly used for listing pages and their updates to search engines. Sitemaps are no more in HTML format, but in plain text format or commonly HTML.

    Google introduced the XML SiteMaps to help web developers to publish list of pages with their recency and update frequency for crawling and indexing. Later Yahoo, Bing and Ask joined with Google in forming a common SiteMap protocol.

    The official website for SiteMap protocol is http://www.sitemaps.org/.

    A sample sitemap XML with just one URL is as follows:

    <?xml version='1.0' encoding='UTF-8'?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
           http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
     <url>
      <loc>http://example.com/</loc>
      <lastmod>2006-11-18</lastmod>
      <changefreq>daily</changefreq>
      <priority>0.8</priority>
     </url>
    </urlset>

     You can generate an ASPX SiteMap for your website with content type text/xml. The following VB.NET methods help you to render Google protocol based sitemaps with any custom url. You can add the URLs manually by writing a line of code for each, or feed it from a database table. If you have dynamic pages in your website, this code will be extremely useful for dynamically rendering a sitemap containing all URLs. 

        Private Sub generateSiteMap(ByVal stream As Stream)
            Dim siteMap As XmlTextWriter
     
            siteMap = New XmlTextWriter(stream, Encoding.UTF8)
     
            siteMap.WriteStartDocument()
            siteMap.WriteStartElement("urlset")
            siteMap.WriteAttributeString("xmlns",
                     "http://www.sitemaps.org/schemas/sitemap/0.9")
            siteMap.WriteAttributeString("xmlns:xsi"
                     "http://www.w3.org/2001/XMLSchema-instance")
            siteMap.WriteAttributeString("xsi:schemaLocation",
                     "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd")
     
            ' Feed all your URLs here
            addStaticPage(siteMap, "/Default.aspx", 1.0)
            ' Add other urls here
     
            siteMap.WriteEndElement()
            siteMap.WriteEndDocument()
            siteMap.Close()
        End Sub
     
        Private Sub addStaticPage(ByVal xWriter As XmlTextWriter, ByVal relUrl As String, ByVal priority As Single)
            If IO.File.Exists(Server.MapPath(relUrl)) Then
                Dim url As String = "http://www.consultsarath.com" + relUrl
                writeItemNode(xWriter, url, IO.File.GetLastWriteTime(Server.MapPath(relUrl)), "daily", priority)
            End If
        End Sub
        Private Sub writeItemNode(ByVal xWriter As XmlTextWriter, ByVal url As String, ByVal lastModified As Date, ByVal changeFrequency As String, ByVal priority As Decimal)
            xWriter.WriteStartElement("url")
            xWriter.WriteElementString("loc", escapeUrl(url))
            xWriter.WriteElementString("lastmod", lastModified.ToString("yyyy-MM-dd"))
            xWriter.WriteElementString("changefreq", changeFrequency)
            xWriter.WriteElementString("priority", priority.ToString("0.#"))
            xWriter.WriteEndElement()
        End Sub
     
        Function escapeUrl(ByVal url As String) As String
            Return url.Replace("&", "&amp;").Replace("'", "&apos").Replace("""", "&quot;").Replace(">", "&gt;").Replace("<", "&lt;")
        End Function

    The above methods constitute a very generic method of generating sitemaps. To embed this into your website and generate sitemap, you need to create a new ASPX page - SiteMap.aspx. In the code view, you can simply add the above codes, customize your URL feeding codes, and include a Page_Load call like this:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.Clear()
            Response.Cache.SetNoStore()
            Response.ContentType = "text/xml"
             generateSiteMap(Response.OutputStream)
             Response.End()
        End Sub

     

    ConsultSarath - We provide end to end outsourcing solutions for .net programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for php programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India. ConsultSarath - We provide end to end outsourcing solutions for python programming requirements- you can hire programmer for hourly rates, for monthly commitments, for short term projects, for long term projects, Contact to know our hourly rates for programmer in India.

    Other Popular Articles
    We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...