Simple Sitemap Generator

How to automatically generate a site map using a T4 text template

When I first created this web site I knew that I needed to create a sitemap.xml file and submit it to Google. This would permit Google to determine which pages I wanted indexed and which page was the canonical version. I did not really want to create one by hand and then have to keep it up to date every time I made changes to the site. I have a very basic site, so it seemed like it would not be a difficult task to automate building the site map. Most of the solutions I found online were overkill for such a simple web site, so I decided to create my own. This article describes my solution so that others might be able to adapt it for use on their own web site. If you have a more complex web site this solution might be too simplified.

Visual Studio is able to create and run T4 text templates, which are a mixture of text and control logic for generating text files. After looking at the capabilities of T4 files I found them to be a perfect solution for generating my site map. The Microsoft documentation for code generation and T4 text templates is located at https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019. To add a T4 text template to a Visual Studio project use the add new item menu and search for text template. That should enable you to add a text template to your project.

The logic I used to generate my site map is pretty simple. Since all of my aspx pages are located in the same folder, I just get a listing of all the aspx pages in that folder. If your web site has sub-folders you will need to include those folders either by processing them explicitly or recursively. There were a few pages I didn't want indexed, so I created an array of pages to exclude from the site map. Then I loop through all the aspx pages. If the page isn't in the exclusion list I generate an entry in the site map.

Here is the code for the T4 file.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".xml" #>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">  
<# 
string[] pagesToExclude = {
    "404Page.aspx",
    "ErrorPage.aspx"
};
DirectoryInfo directoryInfo = new DirectoryInfo(this.Host.ResolvePath(""));
FileInfo[] Files = directoryInfo.GetFiles("*.aspx");
foreach(FileInfo file in Files) { 
if (!pagesToExclude.Contains(file.Name)) { #>
  <url>
<# string fileName = Path.GetFileNameWithoutExtension(file.Name);
if (fileName.Equals("Default")) { fileName = "";} #>
    <loc>https://kb3hha.com/<#= fileName #></loc>
    <lastmod><#= File.GetLastWriteTime(Path.Combine(file.DirectoryName, file.Name)).ToUniversalTime().ToString("yyyy-MM-dd") #></lastmod>
  </url>
<# }
 } #>
</urlset>

If you want to have Visual Studio run the text transformation automatically as part of every build see this article

I hope you find this useful. It works for me.
Written by Seth Cohen on 17-Jul-2020

If you want to encourage me to continue developing software you can