C# Template for Creating Google Video Sitemap

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!

5 thoughts on “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.

Comments are closed.