C# / .NETSecurityDevOps
C# / .NET
Create an XML Sitemap
Alexandru Puiu
Alexandru Puiu
September 16, 2012
1 min

XML sitemaps are accepted by search engines such as Google. Although it will not tell them to crawl pages included in the sitemap, it will tell them how to treat the pages they otherwise crawl, which can be very important in making the search engine understand your website.

We start off with a Sitemap class

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using DataAnnotationsExtensions;
using System.Xml.Serialization;
using System.Collections;

namespace Site.Models
{
    [XmlRoot("urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
    public class Sitemap
    {
        private ArrayList map;
        public Sitemap()
        {
            map = new ArrayList();
        }
        [XmlElement("url")]
        public Location[] Locations
        {
            get
            {
                Location[] items = new Location[map.Count];
                map.CopyTo(items);
                return items;
            }
            set
            {
                if (value == null)
                    return;
                Location[] items = (Location[])value;
                map.Clear();
                foreach (Location item in items)
                    map.Add(item);
            }
        }
        public int Add(Location item)
        {
            return map.Add(item);
        }
    }
    // Items in the shopping list
    public class Location
    {
        public enum eChangeFrequency
        {
            always,
            hourly,
            daily,
            weekly,
            monthly,
            yearly,
            never
        }
        [XmlElement("loc")]
        public string Url { get; set; }
        [XmlElement("changefreq")]
        public eChangeFrequency? ChangeFrequency { get; set; }
        public bool ShouldSerializeChangeFrequency() { return ChangeFrequency.HasValue; }
        [XmlElement("lastmod")]
        public DateTime? LastModified { get; set; }
        public bool ShouldSerializeLastModified() { return LastModified.HasValue; }
        [XmlElement("priority")]
        public double? Priority { get; set; }
        public bool ShouldSerializePriority() { return Priority.HasValue; }
    }
}

Next, create your sitemap in the controller

public void GoogleSitemap()
{
    Sitemap sitemap = new Sitemap();
    sitemap.Add(new Location()
    {
        Url = "http://example.com/",
        LastModified = DateTime.UtcNow.AddDays(-1)
    });
    using (Repository rep = new Repository())
    {
        //foreach static page
        foreach (var page in rep.GetStaticPages())
        {
            sitemap.Add(new Location()
            {
                Url = "http://example.com/" + page.WebName,
                ChangeFrequency = Location.eChangeFrequency.weekly,
                Priority = 0.6D //0.6 weight priority
            });
        }
    }
    Response.Clear();
    XmlSerializer xs = new XmlSerializer(typeof(Sitemap));
    Response.ContentType = "text/xml";
    xs.Serialize(Response.Output, sitemap);
    Response.End();
}

And finally, we tell Google where to look for each sitemap. Note, you can create multiple sitemaps as described above, and it’s recommended you do, if your sitemap gets too big.

You need to create a file called sitemaps.xml, and inside it put the following. (change example.com/sitemap.xml to point to your controller method above)


Tags

utils
Alexandru Puiu

Alexandru Puiu

Engineer / Security Architect

Systems Engineering advocate, Software Engineer, Security Architect / Researcher, SQL/NoSQL DBA, and Certified Scrum Master with a passion for Distributed Systems, AI and IoT..

Expertise

.NET
RavenDB
Kubernetes

Social Media

githubtwitterwebsite

Related Posts

Uploading Large Files
Upload Large Files in C#
September 21, 2020
4 min
© 2022, All Rights Reserved.

Quick Links

Get In TouchAbout Me

Social Media