1

I am doing a module which can display my city, state, latitude and longitude, using IP address of the device I am using. However, I can't get it right. Below, there is my code which I referred to another website:

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

}

All the code has no problem. The only problem would be the _geoLoc, it keep prompt out red lines below each of them. What does it mean? Thank you.

2
  • What is the variable _geoLoc and where is it initialized? Nov 21, 2013 at 9:48
  • If you red line '_geoLoc', that means the variable is not declared or initialized in your class. Nov 21, 2013 at 9:51

2 Answers 2

1

As others have pointed out, _geoLoc isn't defined. Try something like this

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;
    }
}
8
  • thanks for your suggestions. It helped a lot. May I ask that how can I display the results on a TextBlock? Nov 21, 2013 at 10:53
  • Once you have the result from the method call, you can set the text of your TextBlock to use properties on the GeoLoc object.
    – Joe
    Nov 21, 2013 at 11:06
  • is it TextBlock.Text = _geoLoc.City.Value.ToString(); ? Nov 21, 2013 at 13:15
  • If that is what you want to see, try it and see if it works. The GetAttribute method returns a string, so the Value property of City probably doesn't exist. I haven't got all of your code and I don't know what you want, so I can't definitively say that this is correct or not.
    – Joe
    Nov 21, 2013 at 13:19
  • actually I want to display the result after I clicked on a button. So I planned to do it like this: void onButtonClick(object sender, RoutedEventArgs e){ try{ //all the codes above TextBlock.Text = _geoLoc.City; } } is it possible? Nov 21, 2013 at 13:30
0

Seems like you dont have instenciated _geoLoc this should be instenciatted before you use it. try something like.

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;
        }
    }
}
4
  • thanks for your suggestions. May I ask that how can I display the results I get on a TextBlock? Nov 21, 2013 at 10:52
  • override the ToString Method in GeoLoc class and format the data what you want to show as string for this. class. or you can use geoloc.propertyname for what you want to show.
    – JSJ
    Nov 21, 2013 at 11:23
  • is it TextBlock.Text = _geoLoc.City.Value.ToString(); ? Nov 21, 2013 at 13:17
  • its like TextBlock.Text = _geoLoc.City;
    – JSJ
    Nov 21, 2013 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.