Can someone help me out real quick, im writing something that is using the Youtube API and it gives me results in XML, i am trying to get a title of the vid from the XML response.
Here is a link to the XML : https://gdata.youtube.com/feeds/api/videos/T7sIiqq66rk?v=2
Here is my code:
string nameURL = "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2";
WebRequest nameRequest = WebRequest.Create(nameURL);
HttpWebResponse nameResponse = (HttpWebResponse)nameRequest.GetResponse();
Stream nameStream = nameResponse.GetResponseStream();
StreamReader nameReader = new StreamReader(nameStream);
string XML = nameReader.ReadToEnd();
var VideoInfo = from e in XElement.Parse(XML).Elements("entry") select new {Id = e.Element("title").Value};
Console.WriteLine(VideoInfo.FirstOrDefault().Id);
The debugger seems to be breaking on the WriteLine method, saying its null, i have tried some other ways as well but cants seem to get just the title element.
Edit: There is something wrong that’s causing the the results to be null, i have established that, but could someone point out why it keeps returning null.
First of all,
VideoInfo.FirstOrDefault()will return null if there are no entries, so it will throw an exception when you try to access theIdfield.Since
entryis the root element, you should access XDocument.Root, and notElements("entry")Your code should be more like this: