Hi! 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!