Introduction
Sitemaps are an easy way for web masters to inform search engines about pages on their website that needs to be crawled. It is an XML file which consists of lists of URLs with additional meta data on each URL. Sitemaps is arguably one of the most important feature of Site SEO (Search Engine Optimization). With the release of sitemap protocol 0.90, Google has managed to gain support for two other search giants, namely Microsoft and Yahoo!, to use the standard protocol. This module allows you to easily create and modify sitemaps generation for your website.
The C# Sitemap Class
To use the C# sitemap class, all you have to do is to include two .cs files in your project, namely, Sitemap.cs and Url.cs. Url.cs consists of the basic element for individual URL needed in the sitemap. Sitemap.cs is the class that will be generating the sitemap XML file.
Using the Code
First, you'll have to create a Url
object. A Url
object has four properties:
Loc
ChangeFreq
Priority
LastModified
Examples are as below:
url url1 = new url();
url1.loc = "http://www.codeproject.com";
url1.priority = "1";
url1.lastmodifieddatetime = datetime.now;
url1.changefreq = "always";
You'll then need to create the Sitemap
object and use the public Add
method, adding the url1
object you have created. However, remember to put the intended file name as the parameter to the sitemap class constructor. Example:
Sitemap sitemap0 = new Sitemap(txtFilename.Text);
sitemap0.Add(url1);
and in order to add a new URL:
Url url2 = new Url();
url2.Loc = "http://www.codeproject.com/script/PressRelease/pr.asp";
url2.Priority = "0.8";
url2.LastModifiedDateTime = DateTime.Now;
url1.ChangeFreq = "always";
sitemap0.Add(url2);
After adding all the URLs, call the Write
method, and the sitemap file will be generated.
sitemap0.Write();
History
- Rev 0.1 - 11/29/2006 -- Initial revision.