[Skip to Content]

Using YouTube RSS feed to a DataTable c#

Published on Friday 17 August 2018
Categories: Programming and Code |

Code header

I have added a new page to the blog which lists my latest uploads on my YouTube channel. I found that YouTube has a rss feed for channels which can be used in a RSS reader. 

The format of the RSS urls is http://www.youtube.com/feeds/videos.xml?channel_id=UC3rrX8tBXk8eGutDTAHdXDQ with the channel_id being the ID of the selected YouTube channel.

The XML returned is formatted as:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://www.youtube.com/xml/schemas/2015"><link href="http://www.youtube.com/feeds/videos.xml?channel_id=UC3rrX8tBXk8eGutDTAHdXDQ" rel="self" />
<id>yt:channel:UC3rrX8tBXk8eGutDTAHdXDQ</id>
<yt:channelid>UC3rrX8tBXk8eGutDTAHdXDQ</yt:channelid><title></title><link href="https://www.youtube.com/channel/UC3rrX8tBXk8eGutDTAHdXDQ" rel="alternate" />
<author>
<name>Brian Dorey</name>
<uri>
https://www.youtube.com/channel/UC3rrX8tBXk8eGutDTAHdXDQ
</uri>
</author>
<published>2009-07-22T20:26:39+00:00</published>
<entry>
	<id>yt:video:slPZf4s3LC8</id>
	<yt:videoid>slPZf4s3LC8</yt:videoid>
	<yt:channelid>UC3rrX8tBXk8eGutDTAHdXDQ</yt:channelid><title></title><link href="https://www.youtube.com/watch?v=slPZf4s3LC8" rel="alternate" />
	<author>
	<name>Brian Dorey</name>
	<uri>
	https://www.youtube.com/channel/UC3rrX8tBXk8eGutDTAHdXDQ
	</uri>
	</author>
	<published>2018-08-07T21:30:24+00:00</published>
	<updated>2018-08-13T12:41:03+00:00</updated>
	<media:group>
		<media:title>Fire on the side of the Cliff at Swanage</media:title>
		<media:content height="390" type="application/x-shockwave-flash" url="https://www.youtube.com/v/slPZf4s3LC8?version=3" width="640">
		<media:thumbnail height="360" url="https://i4.ytimg.com/vi/slPZf4s3LC8/hqdefault.jpg" width="480">
		<media:description>
		Fire spreading along the cliff at Swanage on the 7th August 2018. Recorded from approx 2 miles away using Canon 7D mk2 and Canon 100-400mm lens.
		</media:description>
		<media:community>
			<media:starrating average="5.00" count="2" max="5" min="1">
			<media:statistics views="547">
		</media:statistics></media:starrating></media:community>
	</media:thumbnail></media:content></media:group>
</entry>
</feed>

To parse this code into a usable format I found a blog post from 2009 on www.surinderbhomra.com which shows how to format a previous version of the YouTube RSS format into a DataTable using C#.

Based on this code, I rewrote the XML node selection parts to work with the new XML format and this returns a DataTable object which can be bound to a repeater or other control.

private DataTable GetYouTubeData(string YouTubeUrl) {
 //Create DataTable to store YouTube channel video list information
 DataTable dtVideos = new DataTable();
 dtVideos.Columns.Add("YouTubeID", typeof(String));
 dtVideos.Columns.Add("Title", typeof(String));
 dtVideos.Columns.Add("Description", typeof(String));
 dtVideos.Columns.Add("LinkURL", typeof(String));
 dtVideos.Columns.Add("Published", typeof(DateTime));
 dtVideos.Columns.Add("Updated", typeof(DateTime));
 dtVideos.Columns.Add("ImageThumbnailURL", typeof(String));
 dtVideos.Columns.Add("AverageRatings", typeof(Double));
 dtVideos.Columns.Add("ViewCount", typeof(Int32));

 DataRow dr;

 //Link to YouTube RSS feed
 using(XmlTextReader rssReader = new XmlTextReader(YouTubeUrl)) {
  XmlDocument xmlDoc = new XmlDocument();
  //Download the XML (via the XmlTextReader)
  xmlDoc.Load(rssReader);

  //Select all nodes starting with "entry"
  XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName("entry");

  // Loop  over each "entry" element found and add tows to datatable
  foreach(XmlNode node in xmlNodeList) {
   try {
    dr = dtVideos.NewRow();
    //Create a new document, to search through each row
    XmlDocument innerXmlDocument = new XmlDocument();
    innerXmlDocument.LoadXml(node.OuterXml);

    // Get video ID
    dr["YouTubeID"] = innerXmlDocument.GetElementsByTagName("id")[0].InnerText.Replace("yt:video:", "");

    // Get video title
    dr["Title"] = innerXmlDocument.GetElementsByTagName("title")[0].InnerText;

    // Get video description
    dr["Description"] = innerXmlDocument.GetElementsByTagName("media:description")[0].InnerText;

    // Get video Link
    dr["LinkURL"] = innerXmlDocument.GetElementsByTagName("link").Item(0).Attributes["href"].Value;

    // Get published date
    dr["Published"] = DateTime.Parse(innerXmlDocument.GetElementsByTagName("published")[0].InnerText);

    // Get Updated date
    dr["Updated"] = DateTime.Parse(innerXmlDocument.GetElementsByTagName("updated")[0].InnerText);

    // Get video thumbnail
    dr["ImageThumbnailURL"] = innerXmlDocument.GetElementsByTagName("media:thumbnail").Item(0).Attributes["url"].Value;

    //Get video rating
    dr["AverageRatings"] = innerXmlDocument.GetElementsByTagName("media:starRating").Item(0).Attributes["average"].Value;

    //Get video statistics views
    dr["ViewCount"] = innerXmlDocument.GetElementsByTagName("media:statistics").Item(0).Attributes["views"].Value;

    // add new row to datatable
    dtVideos.Rows.Add(dr);
   } catch (Exception e) {
    Response.Write(e.Message);
   }
  }

 }
 return dtVideos;

}

To bind this data to a Repeater control you would use:

string url = "https://www.youtube.com/feeds/videos.xml?channel_id=UC3rrX8tBXk8eGutDTAHdXDQ";
repVideoList.DataSource = GetYouTubeData(url);
repVideoList.DataBind();

Permalink


0 Comments



Leave a comment