下面的代码如何在asp.net2.0中实现

本文关键字:net2 asp 实现 代码 | 更新日期: 2023-09-27 18:07:04

由于我使用的是asp.net2.0,当我使用下面的类,我得到以下错误。

Error 1 : The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

我使用的类。我如何在asp.net2.0中使用此代码而不出现任何错误

public static XElement GetGeocodingSearchResults(string address)
{
    // Use the Google Geocoding service to get information about the user-entered address
    // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
    var url = String.Format("http://maps.google.com/maps/api/geocode/xml?  
    address={0}&sensor=false", HttpContext.Current.Server.UrlEncode(address));
    // Load the XML into an XElement object (whee, LINQ to XML!)
    var results = XElement.Load(url);
    // Check the status
    var status =results.Element ("status").Value;
    if (status != "OK" && status != "ZERO_RESULTS")
        // Whoops, something else was wrong with the request...
        throw new ApplicationException("There was an error with Google's Geocoding Service: " + status);
    return results;
}

下面的代码如何在asp.net2.0中实现

var只是右边表达式的实际类型的快捷方式。

public static XElement GetGeocodingSearchResults(string address)
{
    // Use the Google Geocoding service to get information about the user-entered address
    // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
    string url = String.Format("http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=false",
       HttpContext.Current.Server.UrlEncode(address));
    // Load the XML into an XElement object (whee, LINQ to XML!)
    XElement results = XElement.Load(url);
    // Check the status
    string status =results.Element ("status").Value;
    if (status != "OK" && status != "ZERO_RESULTS")
        // Whoops, something else was wrong with the request...
        throw new ApplicationException("There was an error with Google's Geocoding Service: " + status);
    return results;
}

BUT LINQ to XML(和整个LINQ功能)仅在。net 3.5及以上版本中可用。你应该升级到。net 3.5或者切换到System.Xml