解析集合中的 Linq to Objects 错误

发布于 2024-11-06 19:48:00 字数 2322 浏览 0 评论 0原文

我在 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:
enter image description here

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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-11-13 19:48:00

首先在类头中添加对 System.Linq 的引用。如:

using System.Linq;

您的情况的问题是,当未选择任何内容时,您在 foreach 循环的开头设置断点。在循环内创建一个动作并在那里设置断点。您将看到这些值将有一个 Student 实例。

First of all add a reference to System.Linq in the class header. As in:

using System.Linq;

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 a Student instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文