Creating a Dynamic SiteMap page in ASP.NET

Search engines don’t magically discover every corner of your site—they follow the trails you leave for them. A well-formed sitemap.xml is one of the strongest signals you can give Google, Bing, and others about what pages exist, how often they change, and which ones matter most. Instead of maintaining a static XML file by hand, ASP.NET WebForms gives you a clean way to generate a sitemap dynamically, pulling URLs straight from your database and streaming valid XML directly to the browser.

A dynamic sitemap solves a real problem: websites evolve, menus shift, and URLs change. Hard-coding a sitemap means it’s always one deployment behind reality. By generating the XML on the fly, your sitemap always reflects the current state of your content—no manual edits, no stale links, no forgotten pages.

The technique is simple but powerful. Instead of rendering HTML, the page writes XML directly to the response stream. An XmlWriter handles the formatting, indentation, and encoding, while your database provides the list of URLs. Each row becomes a <url> entry with metadata like lastmod, changefreq, and priority. The result is a standards-compliant sitemap that search engines can crawl instantly.

This approach also scales. Whether you have ten pages or ten thousand, the sitemap stays accurate because it’s generated from the same source of truth your site uses to build its navigation. And because the output is pure XML, it’s lightweight, fast, and easy for crawlers to process.

If you ever expand your site into multiple sections, add versioned content, or introduce dynamic routing, this pattern adapts cleanly. You can segment your sitemap into multiple files, generate a sitemap index, or even apply custom priority rules based on database fields. The core idea remains the same: let your application describe itself to search engines in real time.

 

<%@ Page Title="" Language="vb" AutoEventWireup="true" CodeBehind="SiteMap.aspx.vb" Inherits="RealWorldCode.SiteMap" %>

 

Imports System.Xml
Imports System.Text
 
Partial Class sitemap
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
 
        ' Clear any existing output and declare that we are returning XML
        Response.Clear()
        Response.ContentType = "text/xml"
 
        ' Configure the XML writer (pretty formatting + UTF-8)
        Dim settings As New XmlWriterSettings()
        settings.Indent = True
        settings.Encoding = Encoding.UTF8
 
        Using writer As XmlWriter = XmlWriter.Create(Response.OutputStream, settings)
 
            writer.WriteStartDocument()
 
            ' Root <urlset> element required by the sitemap protocol
            writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9")
 
            ' Pull dynamic URLs from the database
            Dim oDT As DataTable = SPs.FP_Menu_SEL_forSiteMap().getTable
 
            For Each oRow As DataRow In oDT.Rows
 
                ' Build the absolute URL for each row
                Dim url As String = "https://realworldcode.com/" & oRow("URL").ToString()
 
                writer.WriteStartElement("url")
 
                ' <loc> — the page URL
                writer.WriteElementString("loc", url)
 
                ' <lastmod> — when the page was last updated (UTC, yyyy-MM-dd)
                writer.WriteElementString("lastmod", DateTime.UtcNow.ToString("yyyy-MM-dd"))
 
                ' <changefreq> — how often the page typically changes
                writer.WriteElementString("changefreq", "weekly")
 
                ' <priority> — relative importance (0.0–1.0)
                writer.WriteElementString("priority", "0.8")
 
                writer.WriteEndElement() ' </url>
 
            Next
 
            writer.WriteEndElement() ' </urlset>
            writer.WriteEndDocument()
 
        End Using
 
        ' Stop ASP.NET from appending anything after the XML
        Response.End()
 
    End Sub
 
End Class

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences