feedback
Dec 31 2007

C# Template for Creating Google Video Sitemap

by John Dyer

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!

Comments

trackback December 31. 2007 13:11

Trackback from DotNetKicks.com

C# Template for Creating Google Video Sitemap

DotNetKicks.com

Tyler S Clark April 10. 2008 00:46

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.

Tyler S Clark

MISS  SONALI    RASAL April 14. 2008 01:31

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

MISS SONALI RASAL

trif3cta August 9. 2008 18:47

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

trif3cta

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading