这是我的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