Monday, March 28, 2011

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!

From stackoverflow
  • "Name" should be "name" - likewise "Location" to "location".

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

0 comments:

Post a Comment