I would like to retrieve the title of a video using the YouTube atom feed here: http://gdata.youtube.com/feeds/api/videos/uRTXEjjrOko I would then like to add this title to my database. How would I do that?
Asked
Active
Viewed 69 times
1 Answers
2
Linq TO XML is the recommended API for working with XML. You would get the title like this:
@using System.Xml.Linq;
@{
var url = "http://gdata.youtube.com/feeds/api/videos/uRTXEjjrOko";
var video = XDocument.Load(url);
XNamespace media = video.Root.GetNamespaceOfPrefix("media");
var title = video.Descendants(media + "title").First().Value;
}
If you wanted to get the link to a thumbnail, you would do this:
var thumbnail = video.Descendants(media + "thumbnail").First().Attribute("url").Value;
There are actually 4 thumbnail links. The code above just grabs the first one. Once you have extracted all the values you want, you can use them along with your SQL in the Database.Execute
method.

Mike Brind
- 28,238
- 6
- 56
- 88
-
Why did you declare media as XNamespace instead of var? Just Curious. – Knox Nov 28 '12 at 20:06
-
No reason really. I just copied that line of code from an old article of mine (couldn't remember off the top of my head how to declare a namespace properly) and didn't spend any time reviewing the resulting code for consistency or anything. – Mike Brind Nov 28 '12 at 22:09