ASP.Net Youtube API Search Keyword

by top54u.com 19 Jul, 2008

In the previous article about Youtube API Search in ASP.Net C# we learnt the use of XmlDataSource control to read the Youtube API Search RSS feed response. In this tutorial we have used the dynamic XmlDataSource Control class object to read the RSS feed of Search results from Youtube API. In C# code you can create an object of XmlDataSource control by using System.Xml namespace. It allows you to access the XmlDataSource control properties such as DataFile and XPath programmatically to parse the RSS formatted Response of Youtube Search API. While reading the XML format as RSS response you need to parse and access the Xml nodes having namespace prefixes such as openSearch and media e.g.: openSearhc:totalResults, media:category etc. To read these xml nodes with namespace prefix XmlNamespaceManager class object is used. XmlNamespaceManager class provides the AddNamespace method to include the namespace name and its XML schema definition URL that allows reading the XML nodes having prefixes. In the following example code you will learn how to pass the user input keyword to the Youtube API search method and retrieve the relevant results.

 

ASP.Net C# Code for Youtube API Search Results

 

// XmlDataSource Control Dynamic Object

XmlDataSource xmlSource = new XmlDataSource();

xmlSource.DataFile = "http://gdata.youtube.com/feeds/api/videos?vq=" + txtKeyword.Text + "&orderby=relevance&start-index=1&max-results=12&alt=rss&format=5";

 

// XmlNamespaceManager initialized by passing the Xml Document NameTable

xmlN = new XmlNamespaceManager( xmlSource.GetXmlDocument().NameTable );

xmlN.AddNamespace("media", "http://search.yahoo.com/mrss/");

xmlN.AddNamespace(
"openSearch", "http://a9.com/-/spec/opensearchrss/1.0/" );

 

// XmlNodeList generated by passing XPath expression and XmlNamespaceManager Object

XmlNodeList xmlNodes = xmlSource.GetXmlDocument().SelectNodes( "rss/channel/item/media:group", xmlN );

 

divResposne.InnerHtml =
"Total Videos Found: <b>" + xmlSource.GetXmlDocument().SelectSingleNode( "rss/channel/openSearch:totalResults", xmlN ).InnerText + "</b>&nbsp;&nbsp;";

 

Repeater1.DataSource = xmlNodes;

Repeater1.DataBind();

 

 

Output:

You can download the above free ASP.Net Youtube API Search script below:

youtube-keyword-search.zip (2.09 kb)

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:29:40 AM




related videos.....
recent posts.....
top54u ezines.....