In this tutorial we will learn the use of ASP.Net C# XmlNamespaceManager and Youtube API RSS feed for data binding the Youtube Feed items with Repeater Control using XmlDataSource. Xml Namespace Manager provides the functionality to read the RSS feed elements with namespace prefix such as media prefix that syndicates the information about Youtube videos along with its thumbnails. To fetch the data from Youtube feed you have to build up API URL along with query parameters to retrieve specific number of results in RSS format.
You can get the Youtube API documentation for standard Youtube feeds at
http://code.google.com/apis/youtube/ developers_guide_protocol.html#Standard_feeds
And query string variables at
http://code.google.com/apis/youtube/ developers_guide_protocol.html#Searching_for_Videos
For binding the standard feed with ASP.Net Repeater control you just need to use following query string parameters with Youtube API path:
http://gdata.youtube.com/feeds/api/standardfeeds/ most_popular?start-index=1&max-results=10&alt=rss
Above Youtube API RSS feed response can be used in the DataFile property of the XmlDataSource control.
E.g.:
<asp:XmlDataSource
ID="XmlDataSource1"
runat="server"
DataFile="http://gdata.youtube.com/feeds/api/
standardfeeds/most_popular
</asp:XmlDataSource>
Here XPath property of XmlDataSource Control is not used coz XmlNamespaceManager class object is required to read the Youtube RSS feed elements with media prefix. XPath Data Binder method is used inside the Repeater control to read the xml data items.
<ItemTemplate>
<div class="container">
<h4>
</h4>
<p align="left">
<img src="<%# XPath("media:thumbnail/@url", xmlN) %>" alt="<%# XPath("media:title", xmlN) %>" hspace="10" />
<br /><br />
<b>Tag(s): </b>
<b>Category(s): </b>
</p>
<div class="clear">
<hr noshade="noshade" />
</div>
</asp:Repeater>
xmlN is an object of XmlNamespaceManager class that is used to add Yahoo media Namespace.
// public object for XmlNamespaceManager
public XmlNamespaceManager xmlN;
{
// XmlNamespaceManager initialized by passing the Xml Document NameTable
xmlN.AddNamespace("media", "http://search.yahoo.com/mrss/");
// XmlNodeList generated by passing XPath expression and XmlNamespaceManager Object
Repeater1.DataSource = xmlNodes;
Repeater1.DataBind();
}
Output:
Download the Free C# Source code for ASP.Net Youtube API Script
youtube-rss.zip (1.64 kb)
Be the first to rate this post
Tags: asp.net 2.0, rss, c#, xml, youtube api, asp.net repeater control, asp.net repeater databinding, asp.net xmldatasource control, asp.net xpath expressions, c# code, c# tutorials, free source code, download sample code, asp.net youtube api rss, asp.net xmlnamespacemanager addnamespace, xml namespace, what is rss for, rss media namesapce, rss opensearch namespace, yahoo.com xml media, a9.com xml opensearch
8/23/2008 8:50:19 PM