返回结果后出现 LINQ to XML NullReferenceException?
我对 .NET 很陌生,对 LINQ 更陌生,我刚刚开始在我正在从事的一个小项目中探索它。
我有一个从应用程序生成的 XML 文件,其中包含一些我想要解析为专有格式并保存在文件中的值。
我能够使一个 LINQ 查询正常工作,将值返回到我的控制台应用程序。然后第二个查询运行,也返回请求的值,但是然后抛出一个NRE!为什么在返回第三个值后它不停止对结果集的迭代?
首先,这是 XML 文件的摘录:
<analyse>
<sample>Leer 12</sample> sample ID and number
<id>Leer</id> sample ID
</analyse>
<results>
<element>
<name>c</name>
<value>0.000186156337031958</value>
</element>
<element>
<name>co2</name>
<value>0.000682099902270885</value>
</element>
<element>
<name>s</name>
<value>0.000121750914950204</value>
</element>
<element>
<name>so3</name>
<value>0.000304028592755094</value>
</element>
</results>
这是第一个 LINQ 查询:
XDocument uniSample = XDocument.Load(limsfile);
var analyse = uniSample.Descendants("analyse").Select(el => new
{
Sample = el.Element("sample").Value,
Id = el.Element("id").Value
});
foreach (var el in analyse)
{
Console.WriteLine("Sample: " + el.Sample);
Console.WriteLine("Sample ID: " + el.Id);
}
这是第二个 LINQ 查询: (请注意我如何必须重命名匿名类型 Value,因为它似乎与某些内容发生冲突)
var results = uniSample.Descendants("element").Select(elements => new
{
Name = elements.Element("name").Value,
Verdi = elements.Element("value").Value
});
foreach (var el in results)
{
Console.WriteLine("Name: " + el.Name);
Console.WriteLine("Value: " + el.Verdi);
}
现在程序的输出看起来像您所期望的那样
Sample: Leer 12
Sample ID: Leer
Name: c
Value: 0.000186156337031958
Name: co2
Value: 0.000682099902270885
Name: s
Value: 0.000121750914950204
Name: so3
Value: 0.000304028592755094
......但是在第三次迭代之后,NRE 被抛出,指向后者的 LINQ 查询。为什么它不像第一次那样退出 foreach 循环?
编辑:实际上是在第四次迭代之后,抱歉混淆了! :) 谢谢!
I'm quite new to .NET and even more so to LINQ, which I have just begun exploring for a small project I'm working on.
I have a XML file generated from an application that contains a few values that I want to parse into a proprietary format and save in a file.
I'm able to make one LINQ query work fine, returning values to my Console app. Then the second query runs, also returns the requested values, but then throws a NRE! Why doesn't it stop iterating over the resultset after the third value has been returned?
First, heres an excerpt of the XML file:
<analyse>
<sample>Leer 12</sample> sample ID and number
<id>Leer</id> sample ID
</analyse>
<results>
<element>
<name>c</name>
<value>0.000186156337031958</value>
</element>
<element>
<name>co2</name>
<value>0.000682099902270885</value>
</element>
<element>
<name>s</name>
<value>0.000121750914950204</value>
</element>
<element>
<name>so3</name>
<value>0.000304028592755094</value>
</element>
</results>
This is the first LINQ query:
XDocument uniSample = XDocument.Load(limsfile);
var analyse = uniSample.Descendants("analyse").Select(el => new
{
Sample = el.Element("sample").Value,
Id = el.Element("id").Value
});
foreach (var el in analyse)
{
Console.WriteLine("Sample: " + el.Sample);
Console.WriteLine("Sample ID: " + el.Id);
}
This is the second LINQ query:
(note how I had to rename the anonynous type Value because it seemed to conflict with something)
var results = uniSample.Descendants("element").Select(elements => new
{
Name = elements.Element("name").Value,
Verdi = elements.Element("value").Value
});
foreach (var el in results)
{
Console.WriteLine("Name: " + el.Name);
Console.WriteLine("Value: " + el.Verdi);
}
Now the output from the program looks like what you'd expect
Sample: Leer 12
Sample ID: Leer
Name: c
Value: 0.000186156337031958
Name: co2
Value: 0.000682099902270885
Name: s
Value: 0.000121750914950204
Name: so3
Value: 0.000304028592755094
...but then after the third iteration, the NRE is thrown, pointing at the latter LINQ query. Why isn't it exiting the foreach loop like it does the first time??
Edit: Actually it's after the FOURTH iteration, sorry for the mixup! :)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查询计算是惰性的——它只会在遍历结果时执行投影。这就是为什么在异常真正遇到问题元素之前您不会看到异常。要强制查询立即计算,您可以调用
ToList
。我强烈怀疑您的
element
元素缺少name
或value
子元素,因此当您尝试取消引用时该子元素的Value
属性,此时您将收到NullReferenceException
。The query evaluates lazily - it's only going to be performing the projection as it goes through the results. That's why you're not seeing the exception until it actually hits the problem element. To force the query to evaluate eagerly, you can call
ToList
.I strongly suspect that you've got an
element
element which is missing either thename
orvalue
sub-element, so when you try to dereference theValue
property of that sub-element, you're getting aNullReferenceException
at that point.你的摘录工作得很好。问题可能出在您的 xml 文件中的其他位置,并且很可能是您的某些元素没有“名称”或“值”子节点。
Your excerpt is working just fine. The problem is probably somewhere else in your xml file and it most likely is that some of your elements does not have either "name" or "value" subnode.
坚持就是胜利!我最终找到了解决方案! 太自豪了! :-)
基本上改变了两行:
所以我现在在 XElement 而不是 XDocument 中加载 XML,这使我能够更直接地查询数据..?
然后我添加了一个调用来专门获取“结果”的后代,这可以使其在应该时“停止”解析。:-)
感谢你们所有的帮助!
Persistance prevails! I found the solution eventually! soo proud! :-)
Basically changed two lines:
So I'm now loading up the XML in an XElement instead of an XDocument, this allowed me to more directly query the data..?
Then I added a call to get Descendants of "results" specifically, and that does the trick for making it "stop" parsing when it should.. :-)
Thanks for all your help guys!