解析集合中的 Linq to Objects 错误
我在 Windows Phone 7 应用程序中尝试使用 Linq 将 Xml 解析为对象时遇到问题。相同的 linq 查询在 silverlight 中也有效。
这是 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>
我拥有的所有代码都在 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)
{ }
}
}
}
我得到的错误非常奇怪(当您快速观看测试时,这是在 foreach 内): System.Collections.Generic.IEnumerator.Current = 无法计算表达式。 System.Collections.Generic.IEnumerator.Current = 'System.Collections.Generic.IEnumerable' 不包含 'System' 的定义,并且没有扩展方法 'System' 接受类型为 'System.Collections.Generic.IEnumerable
In 的 第一个参数foreach 内的平均时间 _student var 在每次迭代中都有正确的值?!这个错误是一个错误吗?或者说它从哪里来?
... :\
编辑:
这是我看到错误的屏幕截图:
学生收集的结果是正确的,但那里的错误让我感到害怕,因为我会推出一个向应用市场申请。
如果有帮助,我正在使用模拟器进行调试。
编辑: 我根据 Desnnis 的回复添加此屏幕截图。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先在类头中添加对 System.Linq 的引用。如:
您的情况的问题是,当未选择任何内容时,您在 foreach 循环的开头设置断点。在循环内创建一个动作并在那里设置断点。您将看到这些值将有一个
Student
实例。First of all add a reference to
System.Linq
in the class header. As in:The problem in your case is that you are setting the breakpoint at the beginning of the
foreach
loop when nothing is selected. Create an action inside the loop and set a breakpoint there. You will see that the values will have aStudent
instance.