ASP.Net Repeater Data Binding with Youtube API RSS

by top54u.com 12 Jul, 2008
Spotlight.....

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:

 

Most Popular Videos Standard Feed of Youtube:

 

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

?start-index=1&max-results=10&alt=rss">

</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.

 

HTML Code for ASP.Net Repeater Control with Youtube Feed Data Items

 

<asp:Repeater ID="Repeater1" runat="server">

<ItemTemplate>

<div class="container">

<h4>

<%# XPath("media:title", xmlN) %>

</h4>

<p align="left">

<img src="<%# XPath("media:thumbnail/@url", xmlN) %>" alt="<%# XPath("media:title", xmlN) %>" hspace="10" />

<%# XPath("media:description", xmlN) %>

<br /><br />

<b>Tag(s): </b>

<%# XPath("media:keywords", xmlN)%>

<br /><br />

<b>Category(s): </b>

<%# XPath("media:category", xmlN)%>

</p>

<div class="clear">

<hr noshade="noshade" />

</div>

 

</div> </ItemTemplate>

</asp:Repeater>

 

xmlN is an object of XmlNamespaceManager class that is used to add Yahoo media Namespace.

 

C# Code for Using XmlNamespaceManager and AddNamespace

 

// public object for XmlNamespaceManager

public XmlNamespaceManager xmlN;



protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

 

// XmlNamespaceManager initialized by passing the Xml Document NameTable

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

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

 

 

// XmlNodeList generated by passing XPath expression and XmlNamespaceManager Object

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

 

Repeater1.DataSource = xmlNodes;

Repeater1.DataBind();

 

}

}

 

 

Output:

 

 

Download the Free C# Source code for ASP.Net Youtube API Script

youtube-rss.zip (1.64 kb)

Add comment


(Will show your Gravatar icon)  

  Country flag

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



Live preview

8/23/2008 8:50:19 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....