Asp.net 无法在Webmatrix站点中调用System.Xml.Linq的Elements()方法

Asp.net 无法在Webmatrix站点中调用System.Xml.Linq的Elements()方法,asp.net,linq,razor,webmatrix,Asp.net,Linq,Razor,Webmatrix,我按照教程进行了操作,但在运行时出现以下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.Gen

我按照教程进行了操作,但在运行时出现以下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?)
这似乎表明元素不是公认的方法,尽管如此

当我显示详细的编译器输出时,其中包括:

C:\Windows\system32>C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe/t:library/utf8output/R:C:\Windows\Microsoft.NET\assembly\GAC\u MSIL\System.Xml.Linq\v4.0\u 4.0.0\uu b77a5c561934e089\System.Xml.Linq.dll

但后来它说:

Microsoft R Visual C编译器版本4.0.30319.17929 适用于Microsoft R.NET Framework 4.5

在我的Webmatrix设置中,它显示我正在使用.NET 4集成版和ASP.NET网页2.0.20710.0

所有这些C代码都在~\App\u 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文件中尝试了一些东西,但没有效果,充其量只能替换这个:

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}).FirstOrDefault();

我会说:

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

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

    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;  

现在它进行编译,但抛出运行时异常:System.NullReferenceException:Object reference未设置为对象的实例。在xdoc.DegenantsValue中执行var maxTemp=from t时,其中t.Parent.Attributetype.Value==最大值选择t;我尝试了你的两个建议。这是因为它没有从XML文档中获取任何信息吗?您可以共享XML文件吗?您将在此处看到XML文件:。现在几乎可以100%工作。只是我先做了一个,再往下做一个,但现在你在选择中有了它,我就把它拿出来了。子孙后代方法仍然存在问题。但这确实超出了我最初的问题。我感谢你的参与。不过,它来晚了一分钟。
    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;