這是我的XML結構的罰球。這是我的代碼來做到這一點:的LINQ to XML讀取方法使用的NullReferenceException C#在VS2017
閱讀從XML:
XDocument rootDocument = XDocument.Load(filePath)
var vul = from r in rootDocument.Descendants("result")
select new
{
Name = r.Element("name").Value
};
創建新的對象:
foreach(var r in vul)
{
Object a = new Object()
{
Name = r.Name
};
}
的問題是,這個調用
時產生一個NullReferenceExceptionrootDocument.Descendants("result")
提前謝謝!
我想你的代碼與XML,它工作正常,我。 –
你檢查過filepath嗎,rootDocuments可能返回null嗎? – hasan
檢查過,rootDocument返回它應該的內容。 @hsnbl – Thomas