如何在asp.net2.0中实现以下代码?

时间:2013-04-02 05:37:16

标签: c# asp.net-2.0

由于我在使用下面的类时使用的是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;
}

1 个答案:

答案 0 :(得分:1)

对于右侧表达式的实际类型,

var只是shortcut

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;
}

LINQ to XML(以及整个LINQ功能)仅适用于.NET 3.5及更高版本。您应该升级到.NET 3.5或switch to System.Xml

相关问题