<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Everything Silverlight &#187; geographic</title>
	<atom:link href="http://www.michaelsnow.com/tag/geographic/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michaelsnow.com</link>
	<description></description>
	<lastBuildDate>Thu, 28 Apr 2011 21:43:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Silverlight Tip of the Day #10 &#8211; Converting Client IP to Geographical Location</title>
		<link>http://www.michaelsnow.com/2010/05/03/silverlight-tip-of-the-day-10-converting-client-ip-to-geographical-location/</link>
		<comments>http://www.michaelsnow.com/2010/05/03/silverlight-tip-of-the-day-10-converting-client-ip-to-geographical-location/#comments</comments>
		<pubDate>Mon, 03 May 2010 16:34:22 +0000</pubDate>
		<dc:creator>Snowman</dc:creator>
				<category><![CDATA[Network]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[geographic]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[location]]></category>

		<guid isPermaLink="false">http://www.silverlightdev.net/?p=136</guid>
		<description><![CDATA[In Tip of the Day #9 I showed you how you can obtain your clients IP addresses through either server side script or a simple web service call. In this tip I will show you how to take that IP address to determine the geographical location of the user that has that IP. To accomplish [...]]]></description>
			<content:encoded><![CDATA[<blockquote></blockquote>
<p>In <a href="http://www.silverlightdev.net/?p=123">Tip of the Day #9</a> I showed you how you can obtain your clients IP addresses through either server side script or a simple web service call. In this tip I will show you how to take that IP address to determine the geographical location of the user that has that IP.</p>
<p>To accomplish this we simply make a <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.95).aspx">WebClient</a> call to a public based web service that will look up the IP address and return the geographical information that is contained in their database. </p>
<p>An example free web service that provides this data is <a title="http://ipinfodb.com" href="http://ipinfodb.com">http://ipinfodb.com</a>. The url we pass is in this format http://ipinfodb.com/ip_query.php?ip=YOURIP. Replace YOURIP with your actually IP, copy and paste this URL into the IE address bar and you will see your results.</p>
<p>To do this through Silverlight I set up a WebClient call with the url in the format I showed you above:</p>
<p><font color="#80ff80"></font></p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">string</span> url = <span class="str">&quot;http://ipinfodb.com/ip_query.php?ip={0}&quot;</span>; </pre>
<pre><span class="kwrd">string</span> reqUrl = <span class="kwrd">string</span>.Format(url, ipAddress); </pre>
<pre class="alt">&#160;</pre>
<pre>WebClient client = <span class="kwrd">new</span> WebClient(); </pre>
<pre class="alt">client.DownloadStringCompleted += <span class="kwrd">new</span> DownloadStringCompletedEventHandler(downloadInfoCompleted); </pre>
<pre>client.DownloadStringAsync(<span class="kwrd">new</span> Uri(reqUrl), <span class="kwrd">null</span>);</pre>
</div>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>&#160;</p>
<p>The method downloadInfoCompleted() is called when the WebClient query has completed. Using XML Linq I extract the data I want from the results as seen here:</p>
<p><font color="#80ff80"></font></p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">void</span> downloadInfoCompleted(<span class="kwrd">object</span> sender, DownloadStringCompletedEventArgs e) </pre>
<pre>{ </pre>
<pre class="alt">    XDocument xml = XDocument.Parse(e.Result); </pre>
<pre>&#160;</pre>
<pre class="alt">    XElement ip = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;Ip&quot;</span>); </pre>
<pre>    XElement countryCode = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;CountryCode&quot;</span>); </pre>
<pre class="alt">    XElement countryName = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;CountryName&quot;</span>); </pre>
<pre>    XElement regionCode = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;RegionCode&quot;</span>); </pre>
<pre class="alt">    XElement regionName = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;RegionName&quot;</span>); </pre>
<pre>    XElement city = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;City&quot;</span>); </pre>
<pre class="alt">    XElement postalCode = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;ZipPostalCode&quot;</span>); </pre>
<pre>    XElement timeZone = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;Timezone&quot;</span>); </pre>
<pre class="alt">    XElement gmtOffset = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;Gmtoffset&quot;</span>); </pre>
<pre>    XElement dstOffset = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;Dstoffset&quot;</span>); </pre>
<pre class="alt">    XElement latitude = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;Latitude&quot;</span>); </pre>
<pre>    XElement longitude = xml.Element(<span class="str">&quot;Response&quot;</span>).Element(<span class="str">&quot;Longitude&quot;</span>); </pre>
<pre class="alt">}</pre>
</div>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>&#160;</p>
<p>Any number of these values could return null. To get the actual value I first check if it is null else I obtain the data from XElement.Value as seen here:</p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">if</span>(<span class="kwrd">null</span> != ip) </pre>
<pre>        IPTB.Text = <span class="str">&quot;Your IP = &quot;</span>+ip.Value.ToString();</pre>
</div>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:1c9bbfc6-4311-4a5f-8e22-669daecc13f6" class="wlWriterEditableSmartContent">
<p>Source: <a href="http://www.michaelsnow.com/wp-content/uploads/2010/06/Tip10_GeoLocation.zip" target="_blank">Tip10_GeoLocation</a></p>
</div>
<p>Demo: </p>
<p>[silverlight: Tip10_GeoLocation.xap]</p>
<p>Thank you,<br />
  <br />&#8211;Mike</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaelsnow.com/2010/05/03/silverlight-tip-of-the-day-10-converting-client-ip-to-geographical-location/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

