Bite my bytes

What I learn by day I blog at night.

  Home :: Contact :: Syndication  
  935 Posts :: 6496 Comments :: 235 Trackbacks

Search

Most popular posts

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:41 AM tours
thanks for your help..

# 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:45 PM indirload
thank you for this code admin

# 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..

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 5 and 1 and type the answer here: