Bite my bytes

What I learn by day I blog at night - A blog from Microsoft Consultant working from Ljubljana, Slovenia

  Home :: Contact :: Syndication  
  999 Posts :: 7691 Comments :: 235 Trackbacks

Search

Most popular posts
in last 180 days

Categories

My Projects

Archives

Stuff


Copyright © by David Vidmar
 
Contact me!
 
LinkedIn Profile
 
 
 

I have developed a ASP.NET HttpHandler in C# that produces a Google sitemap. Originally I did it as a feature request for Subtext but later realized it’s completely generic and reusable.

So I’m also publishing it’s code here: Download .NET 2.0 source code.

If you don’t know what sitemap is – here is a quote from Google site:

The Sitemap Protocol allows you to inform search engines about URLs on your websites that are available for crawling. In its simplest form, a Sitemap that uses the Sitemap Protocol is an XML file that lists URLs for a site. The protocol was written to be highly scalable so it can accommodate sites of any size. It also enables webmasters to include additional information about each URL (when it was last updated; how often it changes; how important it is in relation to other URLs in the site) so that search engines can more intelligently crawl the site.

Here is a sample sitemap for a taste, if you want to know more, be sure to read protocol specification.

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="
http://www.google.com/schemas/sitemap/0.84">
  <url>
    <loc>http://www.example.com/</loc>
    <lastmod>2005-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url> 
</urlset
>

If you wan’t to see the real thing, you can take a look at my blogs sitemap: http://www.vidmar.net/weblog/sitemap.ashx

 


The whole thing is implement as a custom HttpHandler. Let’s do the registration of custom handler at the beginning so we don’t forget it. Put something like this in your web.config HandlerConfiguration section:

<HttpHandler pattern="Sitemap.ashx" type="Subtext.Web.SiteMap.SiteMapHttpHandler, Subtext.Web" handlerType="Direct"/>

I could use XmlWritter or even use WriteLine() method to generate Xml content (some other implementations you’ll find on the net use that approach), but I decided to do it "the right way". I represented the base Url structure with a class and collection as a List<>, decorated those with some Xml serialization attributes so that the class and members names are violating good naming practices.

[XmlElementAttribute(ElementName = "lastmod", DataType="date")]
public DateTime LastModified
{
  get { return lastModified; }
  set { lastModified = value; }
}

When needed I XmlSerialize the class and I get just the right output. Here is a sample – “lastmod” field:

XmlSerializer serializer = new XmlSerializer(typeof(UrlCollection));
XmlTextWriter xmlTextWriter = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
serializer.Serialize(xmlTextWriter, urlCollection);

Usage of the class is really simple... Create instance, fill UrlList with the pages you want to report to google, figure out your priorities and last changed dates and you're set - submit your sitemap to Google and start enjoying better indexation! (is that a even a word?!)

UrlCollection urlCollection = new UrlCollection();
// Let's add home page
Url homePage = new Url(Config.CurrentBlog.HomeFullyQualifiedUrl, DateTime.Now, ChangeFrequency.Daily, 1.0M);
urlCollection.Add(homePage);

And that’s it. I wanted to implement it on si.blogs tonight but I realized it’s .NET 1.1. Bummer. Well, some other winter night, I guess.

Posted on Thursday, January 04, 2007 11:11 PM | Filed under: Developement |

Feedback

# Subtext 1.9.4 - now with sitemap 1/31/2007 9:05 PM Bite my bytes
Subtext 1.9.4 - now with sitemap

# re: Google Sitemaps in C# 2/12/2007 9:42 PM Rudolf Wenger
thank you for this code, great work!

# re: Google Sitemaps in C# 7/30/2008 7:20 PM turkey
thanx docs

# ankara evden eve nakliyat 8/9/2009 1:46 PM ankara nakliyat
ankara evden eve nakliyat

thankss

# re: Google Sitemaps in C# 9/13/2009 9:43 AM tour
thank you for this excellent code, very very great work..


# re: Google Sitemaps in C# 9/13/2009 9:46 AM tekstil
thanx d - ocs textile company

# re: Google Sitemaps in C# 10/5/2009 8:57 PM Mike
Looks good but am I missing something? Where are the Entry and Entries Classes in the source?

# re: Google Sitemaps in C# 11/22/2009 6:26 PM Yazılı Soruları
thank you for this code, great work!

# re: Google Sitemaps in C# 11/22/2009 6:31 PM sexyono
thank you for this code, great work!


# re: Google Sitemaps in C# 11/22/2009 6:50 PM engin
thanx you

# re: Google Sitemaps in C# 11/22/2009 7:09 PM ahmet
nice work thanks admin.

# re: Google Sitemaps in C# 11/22/2009 7:10 PM video-dizi
Thank you for good work admin

# re: Google Sitemaps in C# 11/22/2009 7:30 PM netkral
Very beatiful

# re: Google Sitemaps in C# 11/22/2009 8:54 PM facebook video
nice. i very usefull

# re: Google Sitemaps in C# 11/22/2009 9:05 PM Lida
Thanks for useful C# informations.

# re: Google Sitemaps in C# 11/22/2009 10:38 PM dizi
very good informations admin ;)

# re: Google Sitemaps in C# 11/23/2009 12:22 AM sinema izle
thank you very veri en verisinden good :D

# # re: Google Sitemaps in C# 11/23/2009 12:23 AM Erdek
good works.thank you very much.

# re: Google Sitemaps in C# 11/23/2009 7:35 AM muzik haberleri
nice one. thanks for the article.

# indir.com 11/24/2009 9:19 AM hasan
good. thanks !

# dizi izle 12/26/2009 9:06 AM Yılmaz
very godd..Thankss.Admin

# re: Google Sitemaps in C# 12/26/2009 9:58 PM Lida
thanks for useful informations

# re: Google Sitemaps in C# 12/27/2009 10:52 AM yazılı soruları
Thanks good essay

# re: Google Sitemaps in C# 12/28/2009 1:02 AM sinema izle
i m too glad to meet you again

# re: Google Sitemaps in C# 1/10/2010 9:49 AM yazili sorulari
Very goo.Thanks vidmar

# re: Google Sitemaps in C# 1/11/2010 8:31 AM Lida
Thank you as always so nice to share my wish again, thank you .. You have to be continuously

# lida 1/21/2010 12:30 PM lida
lida you

# re: Google Sitemaps in C# 1/23/2010 1:48 PM turkey tours
thanks for the info...

# lida 2/25/2010 2:20 PM lida
lida zayiflama ile thank you

# re: Google Sitemaps in C# 4/24/2010 6:26 PM yazılı soruları
thanks because vidmar is beatiful and very good

www.odeviindir.com

# re: Google Sitemaps in C# 5/3/2010 2:10 AM sinema
nice article, thanks for sharing, i liked it

# re: Google Sitemaps in C# 5/21/2010 9:59 AM sudoku
cool this is what i was looking for

# re: Google Sitemaps in C# 5/23/2010 9:44 AM harbi-indir
good ty..

# re: Google Sitemaps in C# 10/30/2010 10:18 AM Stefano
Hello great work, .... how can i render something like this <news:news>
<news:publication>
<news:name>The Example Times</news:name>
<news:language>en</news:language>
</news:publication>
<news:access>Subscription</news:access>
ecc.

Thank you for replay

# re: Google Sitemaps in C# 11/16/2010 9:23 PM sikis
hey very good.

# Ankara Evden Eve Nakliyat,nakliye,nakliyeciler,nakliyatçilar,evden eve nakliye,evden eve tasima 1/17/2011 2:47 PM halilogullari
Ankara Evden Eve Nakliyat,nakliye,nakliyeciler,nakliyatçilar,evden eve nakliye,evden eve tasima

# free tv online 7/7/2011 7:29 AM spreety
The television guide for free tv online, free movies online, news, sports, music and more.


# re: Google Sitemaps in C# 1/9/2012 12:28 PM konteyner
its very nice konteyner and amazing project.

# konteyner 1/16/2012 3:50 PM handekahraman
konteyner ürünleri hakkında tüm detaylı bilgiler

# prefabrik 1/16/2012 3:53 PM handekahraman
prefabrik ürünleri hakkında tüm detaylı bilgiler

# ingilizce iş görüşmesi 1/18/2012 8:53 AM emrah
ingilizce iş görüşmesi hakkında tüm bilgiler güncel kaynaklar

# online ingilizce kursu 1/18/2012 8:54 AM online ingilizce kursu
online ingilizce kursu

Comments have been closed on this topic.