2013-04-02 25 views
1

因为我使用asp.net2.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?) 

没有得到任何错误

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

var I类具有used.How可以i的ASP.NET2.0使用此代码只是用于实际类型右侧的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; 
} 

BUTLINQ to XML(和整个LINQ功能)是仅在.NET 3.5以上可用。你应该升级到.NET 3.5或switch toSystem.Xml

+0

我试过了,但我在'XElement.Load(url)'中得到以下错误。错误如下'不能隐式转换类型'System.Xml.Linq .XElement'转换为'string'。存在明确的转换(你是否缺少一个转换?)' – Kannan

+0

如果你可以帮助我写出System.Xml格式的上述代码 – Kannan

+0

请确保你有'XElement results = XElement.Load( url);',not'string results = XElement.Load(url);' –