John Dyer

Technology and web development in curly bracket languages {Javascript, C#, ActionScript}

C# Template for Creating Google Video Sitemap

by John Dyer 31. December 2007 20:48

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

12/31/2007 10:11:44 PM # trackback DotNetKicks.com | Reply
Trackback from DotNetKicks.com

C# Template for Creating Google Video Sitemap
4/10/2008 9:46:03 AM # Tyler S Clark Tyler S Clark United States | Reply
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's last post: tylersclark.com/.../" rel="nofollow">Huge Youngstown weekend: Arts Festival, Cycle Tour
4/14/2008 10:31:27 AM # MISS  SONALI    RASAL MISS SONALI RASAL India | Reply
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
8/10/2008 3:47:41 AM # trif3cta trif3cta United States | Reply
Thanks for posting this, I've been on the hunt for something similar.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Web Statistics