Sean Chambers
Sean Chambers

Reputation: 8680

NullReferenceException when using Linq to XML

Given this xml document:

<projects><project><name>sample project</name><location>http://somewhere.com/</location></project></projects>

And this linq to xml statement for retrieving name/location elements and creating a new Project object:

return xmlDocumentFromAbove.Descendants("project").Select(p => new Project(p.Element("Name").Value, p.Element("Location").Value));

I keep getting a NRE where I am accessing p.Element("Name").Value. Am I missing something obvious here?

Thanks!

Upvotes: 2

Views: 503

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

"Name" should be "name" - likewise "Location" to "location".

return xmlDocumentFromAbove.Descendants("project").Select(p =>
    new  Project(p.Element("name").Value, p.Element("location").Value));

Upvotes: 3

Related Questions