2009-02-04 80 views
2

鉴于此XML文档:的NullReferenceException使用LINQ时,XML

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

这的LINQ to XML语句检索名称/位置元素,并创建一个新的项目对象:

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

我不断收到一个NRE,我正在访问p.Element(“Name”)。Value。我在这里错过了很明显的东西吗

谢谢!

回答

3

“名称”应该是“名称” - 同样“位置”到“位置”。

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