C# Template for Creating Google Video Sitemap

December 31, 2007 | Uncategorized | 4 Comments

Earlier this month, Google introduced “Video Sitemaps” as an extension to the Sitemap Protocol standard. It allows website owners to expose their video content (including embedded video) to be indexed and included in Google’s video search (here is Google’s spec). Embrace and extend ;)

I just added this to Dallas Theological Seminary‘s site: http://www.dts.edu/videositemap.xml and I thought it might help to share the template to speed someone else’s development:

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Text;

namespace YourNamespace
{
    public class VideoSiteMap : IHttpHandler
    {
        public VideoSiteMap()
        {
        } 
        
        public void ProcessRequest(HttpContext context)
        {
            XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8); 
            context.Response.ContentType = "text/xml";

            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument(); 
            writer.WriteStartElement("urlset");
            
            // add namespaces
            writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
            writer.WriteAttributeString("xmlns", "video", null, "http://www.google.com/schemas/sitemap-video/1.0");
            
            // fake loop
            for (int i=0; i<10; i++) {
                writer.WriteStartElement("url");

				// required
                writer.WriteElementString("loc", "http://mysite.com/myplayer.aspx");
                writer.WriteStartElement("video", "video", null); 
                
                // start:optional
                writer.WriteElementString("video", "title", null, "Video Title");
                writer.WriteElementString("video", "description", null, "a great video");
                writer.WriteElementString("video", "thumbnail_loc", null, "http://mysite.com/myvideothumb.jpg");
                writer.WriteElementString("video", "family_friendly", null, "Yes");
                writer.WriteElementString("video", "content_loc", null, "http://mysite.com/myvideo.flv");
                writer.WriteElementString("video", "duration", null, "100");                    
                
                writer.WriteStartElement("video", "player_loc", null);                
                writer.WriteAttributeString("allow_embed", "true");
                writer.WriteString("http://mysite.com/embeddedplayer.swf");
                writer.WriteEndElement(); // video:player_loc
                // end:optional
                
                writer.WriteEndElement(); // video:video
                writer.WriteEndElement(); //url
            }
            
            writer.WriteEndElement(); //urlset 
            writer.WriteEndDocument();
            writer.Close();
        }
        public bool IsReusable
        {
            get { return false; }
        }
    }
}

Just add this class to your web.config and you’re all set:

<system.web>
   <httpHandlers>
      <add verb="*" path="videositemap.xml" type="YourNamespace.VideoSiteMap, YourNamespace"/>
   </httpHandlers>
</system.web>

Hope that helps!

4 Responses to “C# Template for Creating Google Video Sitemap”

  1. I assume you had to compile this and stick it in the bin folder. Did you also have to make any adjustments in IIS for this to work? Once I added the add tag to my web.config file, I’ve just gotten an error on my dev site where I’m testing this out: http://dev.greenenergytv.com

    Thanks for the code; looking forward to hooking this up.

  2. You are welcome to invite me on chat at the MSN and Yahoo id’s for further discussions, clarifications and testing of our services.
    MSN id: sonali.rasal@routesms.com

    Yahoo id :sonalir_routesms@yahoo.com

    Skype id: sonalir_routesms

    Regards

  3. trif3cta says:

    Thanks for posting this, I’ve been on the hunt for something similar.

  4. thank you very muck thanks

Leave a Reply

Hi, I'm John Dyer. In my day job, I build websites and create online seminary software for a seminary in Dallas. I also like to release open source tools including a pretty popular HTML5 video player and build tools that help people find best bible commentaries and do bible study. And just for fun, I also wrote a book on the theology of technology and media.

Fork me on GitHub

Social Widgets powered by AB-WebLog.com.