Monday, 22 December 2008

Creating a dynamic site map using ASP.NET and VS 2008

Site maps are an integral part of Search Engine Optimisation (SEO) and if used along with Google Web Master tools provides an easy and powerful way to automatically get all your pages indexed.

Creating a sitemap XML file by hand is very easy but what if you have a site where content is being added all the time, it would be very time consuming to add a new entry to your site map file everytime a new item was added to your site. This is a requirement a client had q while back and so I took the following steps to create them a dynamic site map and I now use this code all all applicable sites I create.

1. Open up your website project in Visual Studio (I use 2008)

2. Add a new Item to your project, choose "Web Form" and give the file a name (e.g. "sitemap.aspx")

3. Once VS has created and opened your new file remove everything from *.aspx file apart from the page declaration line (enclosed in <% %>)

4. Open the *.aspx.cs file and make sure you have imported the "System.Xml" namespace

5. Add the following code to the "Page_Load"

Response.Clear();

XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("urlset");
xmlWriter.WriteAttributeString("xmlns",
http://www.sitemaps.org/schemas/sitemap/0.9);

xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();


This is the basis of your site map file and all its doing is clearing the Response stream (this is what is going to be written to the browser), creates a new Xml text writer and tells that to write it's output to the response stream.

This means that the actual output will be an XML rather than HTML.

6. For static pages add an entry like the following (replaing the parts between the <> with the apropriate information):

xmlWriter.WriteStartElement("url");
xmlWriter.WriteElementString("loc", "http:///.aspx");
xmlWriter.WriteElementString("changefreq", "weekly");
xmlWriter.WriteEndElement();


Add as many entries like this as you need and feel free to change the "changefreq" value to anything you want.

8. Dynamically add other pages. Now the specifics of this will vary depending on how you store and retrieve information but for my example I have an object named "Article" which holds information about an article (ID, title, body and date created) and I have a collection of "Article" objects stored in a generic list called "ListArticles" which is fed straight from the database.

foreach (Article article in listArticles)
{
xmlWriter.WriteStartElement("url");
xmlWriter.WriteElementString("loc", "http:///ViewArticle.aspx?articleID=" + article.ArticleID.ToString());
xmlWriter.WriteElementString("lastmod", article.DateCreateD.ToString("yyyy-MM-dd"));
xmlWriter.WriteEndElement();
}


As you can see I am looping round all the "Article" objects in my list and writing each one using my Xml Writer to the response stream.



Below is the complete example code:


protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();

XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("urlset");
xmlWriter.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
xmlWriter.WriteStartElement("url");

xmlWriter.WriteElementString("loc", "http:///Default.aspx");
xmlWriter.WriteElementString("changefreq", "daily");
xmlWriter.WriteEndElement();


ListArticles listArticles = new ListArticles();

foreach (Article article in listArticles)
{
xmlWriter.WriteStartElement("url");
xmlWriter.WriteElementString("loc", "http:///ViewArticle.aspx?articleID=" + article.ArticleID.ToString());
xmlWriter.WriteElementString("lastmod", article.DateCreate.ToString("yyyy-MM-dd"));
xmlWriter.WriteEndElement();
}


xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}

No comments:

Post a Comment