Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Software
    • Database Consultancy Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Site Design Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft Office
    • Social Media Management and Advice Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About Us
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Careers
      • Translator English-Portuguese
      • Translator English-Spanish
    • Portfolio
    • Team
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • Chester Copperpot
      • Deepika Bandaru
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
čeština (CS)Deutsch (DE)English (EN-US)English (EN-GB)Español (ES)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

Creation of an XML Sitemap from SQL Server In ASP NET

Creation of an XML Sitemap from SQL Server In ASP NET

About

In the process of designing our Content Management System, we knew that a sitemap file would need to be included to enable fast discovery of content by the various crawlers that search engines use.

As our system already has a database behind it, the obvious choice was to use this data and create the file dynamically.

The system needed to be able to handle lots of pages, so for built in redundancy we have created a sitemap index page which then links into the sitemaps.

XML Sitemap Index

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <sitemap>   

  <loc>https://www.claytabase.co.uk/sitemap.xml?Language=EN&Page=1</loc>

  <lastmod>2019-07-17</lastmod>

</sitemap>

<sitemap>

  <loc>https://www.claytabase.co.uk/sitemap.xml?Language=EN&Page=2</loc>

  <lastmod>2019-07-17</lastmod>

</sitemap>

</sitemapindex>

XML Sitemap

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <url>

    <loc>https://www.claytabase.co.uk/</loc>

    <lastmod>2013-12-13</lastmod>

    <changefreq>monthly</changefreq>

    <priority>1</priority>

  </url>

  <url>

    <loc>https://www.claytabase.co.uk/About-Us</loc>

    <lastmod>2013-12-09</lastmod>

    <changefreq>monthly</changefreq>

    <priority>1</priority>

  </url>

</urlset>

How to generate it

I have split out the code generation into two classes for ease of reading, but it could be combined if you like.

The generation is hooked into the page load event, but is fairly simple.

  • Set the response content type as text/xml and encoding as utf-8
  • Create an XML Text Writer and start writing the document
  • Create an SQL connection using a connection string saved in the web config file
  • Get all of the possible values from your database and push them into a dataset for reading
  • Write the first element (sitemapindex or urlset), this is required as part of the standard, and set the schema
  • Start a loop through the dataset
    • Write the require element (sitemap or url)
    • Write the required attributes for each element
  • Clean up and close the stream

Sitemap Index

Imports System.Data

Imports System.Data.SqlClient

Imports System.Xml

Partial Class Generate_SitemapIndex

   Inherits System.Web.UI.Page

   Dim conStr As String = ConfigurationManager.ConnectionStrings("MySqlConnection").ConnectionString

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Response.Clear()

       Response.ContentType = "text/xml"

       Response.Charset = "Utf-8"

       Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

       xtwFeed.WriteStartDocument()

       Using con As New SqlConnection(conStr)

           Dim com As New SqlCommand("SELECT {SitemapUrl},{SitemapModified} FROM {YourDatabase}", con)

           Dim ds As New DataSet, da As New SqlDataAdapter(com)

           con.Open()

           da.Fill(ds)

           xtwFeed.WriteStartElement("sitemapindex")

           xtwFeed.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")

           Dim dr = ds.Tables(0).CreateDataReader

           While dr.Read

               xtwFeed.WriteStartElement("sitemap")

               xtwFeed.WriteElementString("loc", dr.Item(0).ToString) 'OR full URL from your database!

               xtwFeed.WriteElementString("lastmod", dr.Item(1).ToString) 'ISO1806 format date.

               xtwFeed.WriteEndElement()

           End While

           xtwFeed.WriteEndElement()

       End Using

       xtwFeed.WriteEndDocument()

       xtwFeed.Flush()

       xtwFeed.Close()

       Response.End()

   End Sub

End Class

Sitemap

Imports System.Data

Imports System.Data.SqlClient

Imports System.Xml


Partial Class Generate_Sitemap

   Inherits System.Web.UI.Page

   Dim conStr As String = ConfigurationManager.ConnectionStrings("MySqlConnection").ConnectionString

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Response.Clear()

       Response.ContentType = "text/xml"

       Response.Charset = "Utf-8"

       Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

       xtwFeed.WriteStartDocument()

       Using con As New SqlConnection(conStr)

           Dim com As New SqlCommand("SELECT {PageUrl},{PageModified},{PageChangeFreq},{PagePriority} FROM {YourDatabase}", con)

           Dim ds As New DataSet, da As New SqlDataAdapter(com)

           con.Open()

           da.Fill(ds)

           xtwFeed.WriteStartElement("urlset")

           xtwFeed.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")

           Dim dr = ds.Tables(0).CreateDataReader

           While dr.Read

               xtwFeed.WriteStartElement("url")

               xtwFeed.WriteElementString("loc", dr.Item(0).ToString) 'full URL from your database!

               xtwFeed.WriteElementString("lastmod", dr.Item(1).ToString) 'ISO1806 format date.

               xtwFeed.WriteElementString("changefreq", dr.Item(2).ToString) 'daily, weekly, monthly etc

               xtwFeed.WriteElementString("priority", dr.Item(3).ToString) 0.0 to 1.0

               xtwFeed.WriteEndElement()

           End While

           xtwFeed.WriteEndElement()

       End Using

       xtwFeed.WriteEndDocument()

       xtwFeed.Flush()

       xtwFeed.Close()

       Response.End()

   End Sub

End Class

Author

Helpful?

Please note, this commenting system is still in final testing.

Website design by Claytabase

This is a section of code that has been modified from Ousia Content Management System code, one of the quickest and most optimised systems on the market, part of our website design services.

more: Responsive and fast. Web Development, Design and Hosting with Content Management System
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+15125961417info@claytabase.comClaytabase USA, 501 Congress Avenue, Suite 150, Austin, Texas, 78701, United States

Partnered With

The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader