Click here to Skip to main content
12,756,447 members (30,648 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 8 Dec 2013

IP Location

, 9 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to get the location of a specific IP-address


I tried to find a very simple solution for getting the geo-location for a specific IP-address. Most of all, I wanted to avoid tricky string operations and complexity as shown in articles like this. So I use the XmlReader now and one of the web-services around.

Using the Code

This class does everything with a few lines:

using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace IpLocation
    public class Response
        public string Ip, CountryCode, CountryName, RegionCode, 
        	RegionName, City, ZipCode, Latitude, Longitude;

        public Response()
        public static Response GetIpLocation(string IpAddress)
            XmlSerializer serializer = new XmlSerializer(typeof(Response));
            XmlReader reader = XmlReader.Create(new StringReader(GetIpData(IpAddress)));
            return (Response)serializer.Deserialize(reader);
        private static string GetIpData(string IpAddress)
            var web = new WebClient() { Encoding = Encoding.UTF8 };
            return web.DownloadString(@"" + IpAddress);

Then, based on this tiny class, you simply fill myLocation (X.X.X.X is the IP-address of the location you are looking for) and then use the properties of myLocation, e.g. CountryName, City and all the others: 

IpLocation.Response myLocation = IpLocation.Response.GetIpLocation("X.X.X.X");

Points of Interest

I like it, how XmlReader does everything needed here. This is the result you get from the URL-API:

I was really surprised how elegantly the XmlReader maps the XML-file with the name and the properties of a defined class, when you precisely use the same names in the class as there are in the XML-tags (see class above). In addition to that XmlReader's magic effectiveness the method DownloadString combined with the StringReader spare us all kinds of file-handling.  


  • 2013-12-08: Posted
  • 2013-12-09: Explanation of XmlReader added. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

dietmar paul schoder
United Kingdom United Kingdom
I am a developer and convinced user of Visual Studio, C#, .net, WPF, MVVM, the project management standards of PMI and IPMA - and Microsoft Surface. I live in Eastbourne, United Kingdom.

You may also be interested in...

Comments and Discussions

GeneralCSI should have known this... (fun) Pin
Taco Ditiecher7-Aug-14 22:11
memberTaco Ditiecher7-Aug-14 22:11 
QuestionLove it, but... Pin
SoMad8-Dec-13 11:33
professionalSoMad8-Dec-13 11:33 
AnswerRe: Love it, but... Pin
Dietmar Schoder8-Dec-13 23:00
professionalDietmar Schoder8-Dec-13 23:00 
GeneralRe: Love it, but... Pin
SoMad8-Dec-13 23:12
professionalSoMad8-Dec-13 23:12 
GeneralRe: Love it, but... Pin
Dietmar Schoder8-Dec-13 23:24
professionalDietmar Schoder8-Dec-13 23:24 
GeneralRe: Love it, but... Pin
SoMad8-Dec-13 23:26
professionalSoMad8-Dec-13 23:26 
GeneralRe: Love it, but... Pin
Dietmar Schoder8-Dec-13 23:59
professionalDietmar Schoder8-Dec-13 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 9 Dec 2013
Article Copyright 2013 by dietmar paul schoder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid