コード例 #1
0
        public Coordinate GetLocationByIpAddress(IPAddress ipAddress)
        {
            Coordinate result = new Coordinate();
            string ip = ipAddress.ToString();
            if (!cachedIps.ContainsKey(ip))
            {
                string r = GetLocationInformation(ip);

                var xmlResponse = XDocument.Parse(r);
                var gml = (XNamespace)"http://www.opengis.net/gml";
                var ns = (XNamespace)"http://www.hostip.info/api";

                try
                {
                    result = (from x in xmlResponse.Descendants(ns + "Hostip")
                              select new Coordinate
                              {
                                  Longitude = decimal.Parse(x.Descendants(gml + "coordinates").Single().Value.Split(',')[0]),
                                  Latitude = decimal.Parse(x.Descendants(gml + "coordinates").Single().Value.Split(',')[1])
                              }).SingleOrDefault();
                }
                catch (NullReferenceException)
                {
                    //Looks like we didn't get what we expected.
                }
                if (!result.NotSet())
                {
                    cachedIps.Add(ip, result);
                }
            }
            else
            {
                result = cachedIps[ip];
            }
            return result;
        }