2013-11-21 60 views
1

我正在做一個模塊,可以使用我使用的設備的IP地址顯示我的城市,州,經度和緯度。 但是,我不明白。下面,還有就是我的代碼,我提到另一個網站:使用C#和WPF獲取IP地理位置

internal GeoLoc GetMyGeoLocation() 
{ 
    try 
    { 
     //create a request to geoiptool.com 
     var request = WebRequest.Create(new Uri("http://geoiptool.com/data.php")) as HttpWebRequest; 


    if (request != null) 
    { 
     //set the request user agent 
     request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)"; 

     //get the response 
     using (var webResponse = (request.GetResponse() as HttpWebResponse)) 
      if (webResponse != null) 
       using (var reader = new StreamReader(webResponse.GetResponseStream())) 
       { 

        //get the XML document 
        var doc = new XmlDocument(); 
        doc.Load(reader); 

        //now we parse the XML document 
        var nodes = doc.GetElementsByTagName("marker"); 

        Guard.AssertCondition(nodes.Count > 0,"nodes",new object()); 
        //make sure we have nodes before looping 
        //if (nodes.Count > 0) 
        //{ 
         //grab the first response 
         var marker = nodes[0] as XmlElement; 

         Guard.AssertNotNull(marker, "marker"); 

         //get the data and return it 
         _geoLoc.City = marker.GetAttribute("city"); 
         _geoLoc.Country = marker.GetAttribute("country"); 
         _geoLoc.Code = marker.GetAttribute("code"); 
         _geoLoc.Host = marker.GetAttribute("host"); 
         _geoLoc.Ip = marker.GetAttribute("ip"); 
         _geoLoc.Latitude = marker.GetAttribute("lat"); 
         _geoLoc.Lognitude = marker.GetAttribute("lng"); 
         _geoLoc.State = GetMyState(_geoLoc.Latitude, _geoLoc.Lognitude); 

         return _geoLoc; 
        //} 
       } 
    } 

    // this code would only be reached if something went wrong 
    // no "marker" node perhaps? 
    return new GeoLoc(); 
} 
catch (Exception ex) 
{ 
    throw; 
} 

}

所有的代碼都沒有問題。唯一的問題是_geoLoc,它保持提示下面的每個紅線。這是什麼意思?謝謝。

+0

什麼是變量'_geoLoc'和它在哪裏初始化? –

+0

如果你的紅線'_geoLoc',這意味着變量沒有在你的類中聲明或初始化。 – SridharVenkat

回答

0

正如其他人指出的,_geoLoc沒有定義。嘗試類似這樣的

internal GeoLoc GetMyGeoLocation() 
{ 
    try 
    { 
     //create a request to geoiptool.com 
     var request = WebRequest.Create(new Uri("http://geoiptool.com/data.php")) as HttpWebRequest; 


    if (request != null) 
    { 
     //set the request user agent 
     request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)"; 

     //get the response 
     using (var webResponse = (request.GetResponse() as HttpWebResponse)) 
      if (webResponse != null) 
       using (var reader = new StreamReader(webResponse.GetResponseStream())) 
       { 

        //get the XML document 
        var doc = new XmlDocument(); 
        doc.Load(reader); 

        //now we parse the XML document 
        var nodes = doc.GetElementsByTagName("marker"); 

        Guard.AssertCondition(nodes.Count > 0,"nodes",new object()); 
        //make sure we have nodes before looping 
        //if (nodes.Count > 0) 
        //{ 
         //grab the first response 
         var marker = nodes[0] as XmlElement; 

         Guard.AssertNotNull(marker, "marker"); 

         var _geoLoc = new GeoLoc(); 
         //get the data and return it 
         _geoLoc.City = marker.GetAttribute("city"); 
         _geoLoc.Country = marker.GetAttribute("country"); 
         _geoLoc.Code = marker.GetAttribute("code"); 
         _geoLoc.Host = marker.GetAttribute("host"); 
         _geoLoc.Ip = marker.GetAttribute("ip"); 
         _geoLoc.Latitude = marker.GetAttribute("lat"); 
         _geoLoc.Lognitude = marker.GetAttribute("lng"); 
         _geoLoc.State = GetMyState(_geoLoc.Latitude, _geoLoc.Lognitude); 

         return _geoLoc; 
        //} 
       } 
      } 

     // this code would only be reached if something went wrong 
     // no "marker" node perhaps? 
     return new GeoLoc(); 
    } 
    catch (Exception ex) 
    { 
     throw; 
    } 
} 
+0

感謝您的建議。它幫助了很多。請問我該如何在TextBlock上顯示結果? – user3016854

+0

從方法調用獲得結果後,可以將TextBlock的文本設置爲在GeoLoc對象上使用屬性。 – Joe

+0

是否TextBlock.Text = _geoLoc.City.Value.ToString(); ? – user3016854

0

好像你沒有instenciated _geoLoc這應該在你使用它之前instenciatted。嘗試類似的東西。

namespace MyNamespace 
{ 
    public class GeoLoc 
    { 
     public string City { get; set; } 

     public string Country { get; set; } 

     public string Code { get; set; } 

     public string Host { get; set; } 

     public string Ip { get; set; } 

     public string Latitude { get; set; } 

     public string Lognitude { get; set; } 

     public object State { get; set; } 
    } 
    public class TestGEO 
    { 
     internal GeoLoc GetMyGeoLocation() 
     { 
      GeoLoc _geoLoc = new GeoLoc(); 
      try 
      { 
       //create a request to geoiptool.com 
       var request = WebRequest.Create(new Uri("http://geoiptool.com/data.php")) as HttpWebRequest; 


       if (request != null) 
       { 
        //set the request user agent 
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)"; 

        //get the response 
        using (var webResponse = (request.GetResponse() as HttpWebResponse)) 
         if (webResponse != null) 
          using (var reader = new StreamReader(webResponse.GetResponseStream())) 
          { 

           //get the XML document 
           var doc = new XmlDocument(); 
           doc.Load(reader); 

           //now we parse the XML document 
           var nodes = doc.GetElementsByTagName("marker"); 

           // Guard.AssertCondition(nodes.Count > 0, "nodes", new object()); 
           //make sure we have nodes before looping 
           //if (nodes.Count > 0) 
           //{ 
           //grab the first response 
           var marker = nodes[0] as XmlElement; 

           // Guard.AssertNotNull(marker, "marker"); 

           //get the data and return it 
           _geoLoc.City = marker.GetAttribute("city"); 
           _geoLoc.Country = marker.GetAttribute("country"); 
           _geoLoc.Code = marker.GetAttribute("code"); 
           _geoLoc.Host = marker.GetAttribute("host"); 
           _geoLoc.Ip = marker.GetAttribute("ip"); 
           _geoLoc.Latitude = marker.GetAttribute("lat"); 
           _geoLoc.Lognitude = marker.GetAttribute("lng"); 
           _geoLoc.State = GetMyState(_geoLoc.Latitude, _geoLoc.Lognitude); 

           return _geoLoc; 
           //} 
          } 
       } 

       // this code would only be reached if something went wrong 
       // no "marker" node perhaps? 
       return _geoLoc; 
      } 
      catch (Exception ex) 
      { 
       throw; 
      } 
     } 

     private object GetMyState(string p, string p_2) 
     { 
      ///do somting 
      return "State Name"; 
      //return you data; 
     } 
    } 
} 
+0

感謝您的建議。請問如何顯示我在TextBlock上得到的結果? – user3016854

+0

覆蓋GeoLoc類中的ToString方法,並將數據格式設置爲字符串。類。或者您可以使用geoloc.propertyname來顯示您想要的內容。 – JSJ

+0

是否TextBlock.Text = _geoLoc.City.Value.ToString(); ? – user3016854