无法使用 IEnumerable Loop C# 访问 XDocument
Cannot access XDocument with IEnumerable Loop C#
我正在尝试使用 System.Xml.Linq
从 XML 文档加载一些数据,但是当我尝试使用 IEnumerable 对象和 from item in doc
语句时,出现以下错误:
Could not find an implementation of the query pattern for source type 'XDocument'. 'Select' not found.
这是代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using System.Xml.Linq;
public class DataManager
{
// GET: Data
private void LoadTestData()
{
IEnumerable<string> users;
XDocument testData = XDocument.Load(@"C:\Users\gross\source\repos\Database Sandbox\Database Sandbox\App_Data\TestData.xml");
users = from user in testData;
}
}
我环顾四周,但我看到的每个解决方案都只是 "remember to have the using System.Linq statement"
该项目是在 Visual Studio 2017 年使用 ASP.NET + MVC 5
建立的
XDocument 没有实现 IEnumerable。它公开了几种方法来访问 return IEnumerables 之类的文档结构,例如 XElement 或 XAttribute。
例如 testData.Elements() 或 testData.Descendents()
这些将公开您可以使用 Linq 的集合
我正在尝试使用 System.Xml.Linq
从 XML 文档加载一些数据,但是当我尝试使用 IEnumerable 对象和 from item in doc
语句时,出现以下错误:
Could not find an implementation of the query pattern for source type 'XDocument'. 'Select' not found.
这是代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using System.Xml.Linq;
public class DataManager
{
// GET: Data
private void LoadTestData()
{
IEnumerable<string> users;
XDocument testData = XDocument.Load(@"C:\Users\gross\source\repos\Database Sandbox\Database Sandbox\App_Data\TestData.xml");
users = from user in testData;
}
}
我环顾四周,但我看到的每个解决方案都只是 "remember to have the using System.Linq statement"
该项目是在 Visual Studio 2017 年使用 ASP.NET + MVC 5
建立的XDocument 没有实现 IEnumerable。它公开了几种方法来访问 return IEnumerables 之类的文档结构,例如 XElement 或 XAttribute。
例如 testData.Elements() 或 testData.Descendents()
这些将公开您可以使用 Linq 的集合