Here is a working example.
There are a lot of paid as well as free services that can find Visitor Geographical information like country, region, city, latitude, longitude, ZIP code, time zone etc from I.P Addresses. http://iplocationtools.com/ip_location_api.php is one such free IP Geolocation API that returns geographical data in three formats: XML, JSON and CSV. In this article, we will consume this API and fetch visitor geographical information in XML format in the simplest possible way, using LINQ To XML.
Using the IP Address Location XML API is easy. Just specify the I.P. address as shown below http://iplocationtools.com/ip_query2.php?ip=74.125.45.100 and you will receive an XML document with the visitor geographical information in the following format:
<Locations>
<Location id="0">
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>06</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipPostalCode>94043</ZipPostalCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.057</Longitude>
</Location>
</Locations>
In order to specify multiple I.P(maximum 25), use a comma(,) separator as shown here: http://iplocationtools.com/ip_query2_country.php?ip=74.125.45.100,206.190.60.37
We will use the XDocument class to consume the XML API in just two lines of code.
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml.Linq" %>
<script runat="server">
Protected Sub btnGetLoc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetLoc.Click
Dim url As String = String.Empty
If txtIP.Text.Trim() <> String.Empty Then
url = String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim())
Dim xDoc As XDocument = XDocument.Load(url)
If xDoc Is Nothing Or xDoc.Root Is Nothing Then
Throw New ApplicationException("Data is not Valid")
End If
Xml1.TransformSource = "IP.xslt"
Xml1.DocumentContent = xDoc.ToString()
End If
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>IP Address Location</title>
<style type="text/css">
body
{
font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
background-color: #ffffff;
color: #4f6b72;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="panelLoc" runat="server">
<asp:TextBox ID="txtIP" runat="server"></asp:TextBox>
<asp:Button ID="btnGetLoc" runat="server" Text="Get IP Details" />
<br />
<asp:Xml ID="Xml1" runat="server"></asp:Xml>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="updProgress" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
<ProgressTemplate><img alt="progress" src="/images/progress.gif"/> </ProgressTemplate>
</asp:UpdateProgress>
</div>
</form>
</body>
</html>
Our XSLT (IP.xslt) will look similar to the following:
<?xmlversion='1.0'?>
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0">
<xsl:templatematch="/">
<HTML>
<BODY>
<TABLEcellspacing="3"cellpadding="8">
<TR>
<TDclass="heading">
<B>Ip</B>
</TD>
<TDclass="heading">
<B>Status</B>
</TD>
<TDclass="heading">
<B>CountryCode</B>
</TD>
<TDclass="heading">
<B>CountryName</B>
</TD>
<TDclass="heading">
<B>RegionCode</B>
</TD>
<TDclass="heading">
<B>RegionName</B>
</TD>
<TDclass="heading">
<B>City</B>
</TD>
<TDclass="heading">
<B>ZipPostalCode</B>
</TD>
<TDclass="heading">
<B>Latitude</B>
</TD>
<TDclass="heading">
<B>Longitude</B>
</TD>
</TR>
<xsl:for-eachselect="Locations/Location">
<TRbgcolor="#C1DAD7">
<TDwidth="5%"valign="top">
<xsl:value-ofselect="Ip"/>
</TD>
<TDwidth="5%"valign="top">
<xsl:value-ofselect="Status"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="CountryCode"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="CountryName"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="RegionCode"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="RegionName"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="City"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="ZipPostalCode"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="Latitude"/>
</TD>
<TDwidth="10%"valign="top">
<xsl:value-ofselect="Longitude"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>