I have an issue while trying to parse an Xml to Objects using Linq in a Windows Phone 7 application. The same linq query works in silverlight.
Here is the xml:
<?xml version="1.0" encoding="utf-8" ?>
<students>
<student>
<firstName>John</firstName>
<lastName>Doe</lastName>
</student>
<student>
<firstName>Jane</firstName>
<lastName>Doe</lastName>
</student>
</students>
And all the code that I have is in the MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
namespace WindowsPhoneApplication2
{
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
XDocument doc = XDocument.Load("my1.xml");
var test = from students in doc.Elements("students").Elements("student")
select new Student()
{
FirstName = students.Element("firstName").Value,
LastName = students.Element("lastName").Value
};
foreach (var _student in test)
{ }
}
}
}
The error that I get is pretty weird (this is inside the foreach when you quick watch test):
System.Collections.Generic.IEnumerator.Current = Could not evaluate expression.
System.Collections.Generic.IEnumerator.Current = ‘System.Collections.Generic.IEnumerable’ does not contain a definition for ‘System’ and no extension method ‘System’ accepting a first argument of type ‘System.Collections.Generic.IEnumerable
In the mean time inside the foreach the _student var has the correct value on each iteration?! Is this error a bug? Or where is it coming from?
… :\
Edit:
Here is a screenshot of where I see the error:

The students collection turn out to be correct but having that error there frightens me for when i will push out an application to the App Market.
If it helps i am using the emulator to debug.
EDIT:
I’m adding this screenshot based on Desnnis’s response.

First of all add a reference to
System.Linqin the class header. As in:The problem in your case is that you are setting the breakpoint at the beginning of the
foreachloop when nothing is selected. Create an action inside the loop and set a breakpoint there. You will see that the values will have aStudentinstance.