【问题标题】:LINQ to XML - VB.NET to c#LINQ to XML - VB.NET to c#
【发布时间】:2012-12-17 09:27:11
【问题描述】:

在我的项目中有以下内容,用 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("预测").Elements("表格").Elements("时间")

错误 5 找不到查询模式的实现 源类型 'System.Collections.Generic.IEnumerable'。 未找到“选择”。您是否缺少参考或 using 指令 对于'System.Linq'? c:\users\\documents\visual studio 2012\Projects\Database1\Database1\SqlStoredProcedure1.cs 32 30 数据库1

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

【问题讨论】:

  • 附带说明:在 VB 中,您可以将其缩短为 From item In rssFeed.<forecast>.<tabular>.<time> Select New WetherItem With {.time = item.<windDirection>.@code}

标签: c# vb.net linq-to-xml


【解决方案1】:

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

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

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

【讨论】:

  • 我跑步。 NET 4.5,目标平台到 SQL Server 2012。我在参考列表中找不到 System.Linq。顺便说一句,这是一个 SQL CLR
  • 您需要在 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5 中添加对System.Core.dll 的引用,您可能会看到:social.msdn.microsoft.com/Forums/en/vstsdb/thread/…
  • ---------------------------- Microsoft Visual Studio ------------ --------------- 不能引用“C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Core.dll”被添加。该组件已被构建系统自动引用。 - - - - - - - - - - - - - - 好的 - - - - - - - - - - - -----
【解决方案2】:

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

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

【讨论】:

  • 我现在有 :) 错误 4 找不到源类型“System.Collections.Generic.IEnumerable”的查询模式的实现。未找到“选择”。您是否缺少“System.Linq”的引用或使用指令? c:\users\\documents\visual studio 2012\Projects\Database1\Database1\SqlStoredProcedure1.cs 34 26 Database1
【解决方案3】:

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

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

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

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


更多帮助参考这篇文章:-

problem with using refrence to System.Xml.Linq

【讨论】:

    猜你喜欢
    • 2010-10-22
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多