How-to parse YouTube feed using C#

parse youtube rss feed c#

Sometines you need to display RSS feed on your site in fancy way how it looks on official youtube site (see top picture). Well today I can show you how to do this using C# and Visual Studio.

Firt of all we need to obtain an rss feed link. To get it please review official documentation here. In this example we will retrieve videos uploaded by specific user. Link should look like this:

http://gdata.youtube.com/feeds/api/users/username/uploads

To display parsed data on page we will need next controls:

<asp:XmlDataSource ID="YoutubeXmlDataSource" runat="server" XPath="/rss/channel/item"
    DataFile="http://gdata.youtube.com/feeds/api/users/epavlov29/uploads?start-index=1&max-results=4&alt=rss"></asp:XmlDataSource>

<asp:Repeater ID="YoutubeRepeater" runat="server">
    <ItemTemplate>
        <div class="video_thumbnail">
        	<a href='<%# XPath("media:player /@url", xmlN) %>' target="_blank"><img src='<%# XPath("media:thumbnail/@url", xmlN) %>' width="120" height="90" /></a>
        </div>
        <div class="video_description">
        	<a href='<%# XPath("media:player /@url", xmlN) %>' target="_blank"><%# XPath("media:title", xmlN) %></a><br />
            <asp:HiddenField ID="DurationInSeconds" Value='<%# XPath("media:content/@duration", xmlN)%>' runat="server" />
            <asp:Label ID="DurationLabel" runat="server" Text=""></asp:Label>
        </div>
    </ItemTemplate>
</asp:Repeater>

And back-end code to get rss:


// Public object for XmlNamespaceManager
public XmlNamespaceManager xmlN;

///

/// Gets and parse Youtube xml feed
///

private void GetYoutubeRss()
{
// XmlNamespaceManager initialized by passing the Xml Document NameTable
xmlN = new XmlNamespaceManager(YoutubeXmlDataSource.GetXmlDocument().NameTable);
xmlN.AddNamespace(“media”, “http://search.yahoo.com/mrss/”);

// XmlNodeList generated by passing XPath expression and XmlNamespaceManager Object
XmlNodeList xmlNodes = YoutubeXmlDataSource.GetXmlDocument().SelectNodes(“rss/channel/item/media:group”, xmlN);
YoutubeRepeater.DataSource = xmlNodes;
YoutubeRepeater.DataBind();

foreach (Control item in YoutubeRepeater.Items)
{
HiddenField dur = (HiddenField)item.FindControl(“DurationInSeconds”);
int duration = Convert.ToInt32(dur.Value);

Label lb = (Label)item.FindControl(“DurationLabel”);
lb.Text = FormatDuration(duration);
}
}

///

/// Formats duration in seconds into redable form
///

/// Video time in seconds /// Video time like string: (02:35)
private string FormatDuration(int duration)
{
int minutes = (duration / 60);
int seconds = (duration % 60);

string min;
string sec;
if (minutes < 10) { min = "0" + minutes.ToString(); } else { min = minutes.ToString(); } if (seconds < 10) { sec = "0" + seconds.ToString(); } else { sec = seconds.ToString(); } return "(" + min + ":" + sec + ")"; } [/csharp] Quick explanation of the code above. There is are two methods, one is GetYoutubeRss() which is retrieving XML data from provided link and binding it to XML Data Source. Also I create method called FormatDuration() which get video duration in seconds and format it in human readable way, i.e. 02:37.

The approximate result of code above will looks like this:

youtube rss parse result example c#

To get more description data to display, you can just open feed link in browser, explore it and add to page using xPath.

Hope it will help you someday. Ask any question below and enjoy the programming! 😉

3,116 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *