XML:
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
...
<author>
<name>Seamless</name>
<uri>https://gdata.youtube.com/feeds/api/users/SeamlessR</uri>
</author>
Code:
var ns = XNamespace.Get("http://www.w3.org/2005/Atom");
var name = xDoc.Descendants(ns + "entry").First().Element("author").Element("name").Value;
it says Object reference not set to an instance of an object.
You're using Element("author")
and Element("name")
as if they're not in a namespace. The namespace will have been defaulted to the same namespace as entry
, so you want:
var ns = "http://www.w3.org/2005/Atom"; // Just use an implicit conversion
var name = xDoc.Descendants(ns + "entry")
.First()
.Element(ns + "author")
.Element(ns + "name")
.Value;
See more on this question at Stackoverflow