【问题标题】:Can't call the Elements() method for System.Xml.Linq in Webmatrix site无法在 Webmatrix 站点中调用 System.Xml.Linq 的 Elements() 方法
【发布时间】:2013-09-16 20:43:09
【问题描述】:

我按照教程 Consuming and Storing Data from a REST Service with ASP.NET Razor 进行操作,但在运行它时出现此 ASP.NET 错误:

CS1061: 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'Elements' and no extension method 'Elements' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' could be found (are you missing a using directive or an assembly reference?)

参考这一行:

var maxTemp = from t in xdoc.Descendants("temperature").Elements("value")
              where t.Parent.Attribute("type").Value == "maximum"
              select t;

这似乎表明Elements() 不是公认的方法,即使Microsoft says it is

当我Show Detailed Compiler Output 时,它会说:

C:\Windows\system32&gt; "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /t:library /utf8output /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll"

但后来它说:

Microsoft (R) Visual C# Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

在我的 Webmatrix Settings 中,它显示我正在使用 .NET 4 (Integrated)ASP.NET Web Pages 2.0.20710.0

所有这些 C# 代码都在 ~\App_Data\Weather.cshtml 文件的 @functions{} 块中。

我的Default.cshtml 文件包含以下内容:

@using System.Xml.Linq
@{
var temp = Weather.GetWeather("98052");
}
<ol>
    <li>Zip code: @temp.Zip</li>
    <li>High: @temp.MaxTemp</li>
    <li>Low: @temp.MinTemp</li>
    <li>Forecast: @temp.Forecast</li>
    <li>Longitude: @temp.Longitude</li>
    <li>Latitude: @temp.Latitude</li>
</ol>

我做错了什么?

(顺便说一句,我遵循了教程末尾的建议,但它们也没有用。我昨天在谷歌上搜索了几个小时,并在 web.config 文件中尝试了一些东西,但充其量没有帮助)

【问题讨论】:

    标签: asp.net linq razor webmatrix


    【解决方案1】:

    替换这个:

    var maxTemp = from t in xdoc.Descendants("temperature").Elements("value") where t.Parent.Attribute("type").Value == "maximum" select t;
    

    与:

    var maxTemp = from t in xdoc.Descendants("temperature")
                      where t.Attribute("type").Value == "maximum" 
                      select new{value= t.Element("value").Value};
    

    如果你想第一条记录试试这个:

    var maxTemp = (from t in xdoc.Descendants("temperature")
                      where t.Attribute("type").Value == "maximum" 
                      select new{value= t.Element("value").Value}).FirstOrDefault();
    

    【讨论】:

    • 现在它可以编译但它会抛出运行时异常:System.NullReferenceException: Object reference not set to an instance of an object. 执行 var maxTemp = from t in xdoc.Descendants("value") where t.Parent.Attribute("type").Value == "maximum" select t; 时(我尝试了你的两个建议)。是因为它没有从 XML 文档中得到任何东西吗?
    • 可以分享xml文件吗
    • 您将在此处查看 XML 文件:weather service
    • 现在几乎可以 100% 工作。只是我在下面做FirstOrDefault(),但现在你在select 中有它,我把它拿出来了。 Descendants() 方法仍有问题。
    • 但这确实超出了我最初的问题。
    【解决方案2】:

    我会选择以下内容:

    int maxTemp = (int)xdoc.Root
                           .Element("data")
                           .Element("parameters")
                           .Elements("temperature")
                           .FirstOrDefault(t => (string)t.Attribute("type") == "maximum")
                           .Element("value");
    

    【讨论】:

    • 感谢您的加入。不过,它来晚了。
    【解决方案3】:

    我用以下代码替换了代码,之后它运行良好:

        var maxTemp = from XElement in xdoc.Descendants("temperature").Elements("value")
                    where XElement.Parent.Attribute("type").Value == "maximum"
                    select XElement;  
    
        var minTemp = from XElement in xdoc.Descendants("temperature").Elements("value")
                    where XElement.Parent.Attribute("type").Value == "minimum"
                    select XElement;  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 2012-03-17
      相关资源
      最近更新 更多