LINQ to XML - VB.NET to c#



在我的项目中有以下内容,用 VB.NET 编写:

 Dim items = From item In rssFeed.Elements("forecast").Elements("tabular").Elements("time") _
                    Select New WetherItem With {.time=item.Element("windDirection").Attribute("code").Value }

这是怎么用 C# 写的?

我已经尝试了以下方法...

XElement rssFeed = XElement.Load(@"http://www.yr.no/sted/Norge/Rogaland/Karm%C3%B8y/Torvastad/varsel.xml");
var items = from item in rssFeed.Elements("forecast").Elements("tabular").Elements("time")
select new WetherItem { .time=item.Element("windDirection").Attribute("code").Value };

但是我收到错误消息来自

rssFeed.Elements("forecast").元素("表格")。元素("时间")

错误 5 找不到查询模式的实现 源类型 'System.Collections.Generic.IEnumerable'. 未找到"选择"。 是否缺少引用或使用指令 对于"系统林克"? C:\用户\\文档\Visual Studio 2012\项目\数据库1\数据库1\SqlStorageProcedure1.cs 32 30 数据库1

我参考了System.XML.Linq和System.Data.Linq

在顶部的 C# 代码文件中,您将看到 using 语句,其中添加:

using System.Linq;
using System.Xml.Linq;

如果属性名称为 time,则还要从 select 语句的.time中删除.

记得在文件顶部添加 using 语句,并在 c# 中添加查询:

var items = from item in rssFeed.Elements("forecast".Elements("tablular").Elements("time")
            select new WetherItem { 
                           time = item.Element("windDirection").Attribute("code").Value
                       };

这是一个非常不寻常的问题,即使是我在开发应用程序时也遇到了这个问题,因为 VS 都没有明确说明问题是什么,也没有适当的问题。一个,我认为可能是:-

为您的项目添加另一个引用:-使用System.Linq;除了使用System.Xml.Linq;

using System.Linq;
using System.Xml.Linq;

错误可能会消失,项目将构建。


如需更多帮助,请参阅本文:-

使用对 System.Xml.Linq 的引用时出现问题

最新更新