2009-02-04 81 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));