C# 网络编程之通过ip地址获取地理位置(补充)

前面我写过一篇文章"C# 网络编程之获取本机名、ip地址、域名、物理位置"里面可以根据输入的网址根据其ip地址获取器物理位置,其中该部分主要代码是通过有道网提供的在线第三方接口实现动态获取它的数据.引用popping_dancer的博客代码,其主要代码如下图所示:

/// <SUMMARY> 
/// 根据IP 获取物理地址 
/// </SUMMARY> 
/// <PARAM name="strIP"></PARAM> 
/// <RETURNS></RETURNS> 
public static string GetstringIpAddress(string strIP) //strIP为IP 
{ 
    string sURL = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + strIP + ""; 
    string stringIpAddress = "";                     //地理位置 
    using (XmlReader read = XmlReader.Create(sURL))  //获取youdao返回的xml格式文件内容 
    { 
        while (read.Read())                          //从流中读取下一个字节 
        { 
            switch (read.NodeType) 
            { 
                case XmlNodeType.Text:               //取xml格式文件当中的文本内容 
                    if (string.Format("{0}", read.Value).ToString().Trim() != strIP) 
                    { 
                        stringIpAddress=string.Format("{0}", read.Value).ToString().Trim(); 
                    } 
                    break; 
            } 
        } 
    } 
    return stringIpAddress; 
}

当时获取的结果如下图所示,但是可能由于有道已经删除该URL网址,现在已经不能通过该接口获取地理位置.所有我又提供了一种新的方法,希望能帮助到大家和那位朋友.

C# 网络编程之通过ip地址获取地理位置(补充)

由于能力有限,只会通过访问第三方接口获取物理地址,而且很大程度上取决于该网站提供的库数据,如果采用访问本地静态的库查找相应的地理位置,也不太适用.现在访问的网址是:http://www.freegeoip.net/xml/

C# 网络编程之通过ip地址获取地理位置(补充)

源代码及显示的结果如下所示:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml;     //Xml文档 
  
namespace GetLocation 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            string strIP = "74.125.31.104";                            //ip地址 
            string strURL = "http://www.freegeoip.net/xml/" + strIP;   //网址URL 
            string Location = "";                                      //物理位置 
            //通过GetElementsByTagName获取标签结点集合 
            XmlDocument doc = new XmlDocument();                     //Xml文档 
            doc.Load(strURL);                                        //加载strURL指定XML数据 
            XmlNodeList nodeLstCity = doc.GetElementsByTagName("City"); //获取标签 
            Location = "获取单个物理位置:" + nodeLstCity[0].InnerText + ""; 
            Console.WriteLine(Location); 
            //通过SelectSingleNode匹配匹配第一个节点 
            XmlNode root = doc.SelectSingleNode("Response"); 
            if (root != null) 
            { 
                string CountryName = (root.SelectSingleNode("CountryName")).InnerText; 
                string RegionName = (root.SelectSingleNode("RegionName")).InnerText; 
                string City = (root.SelectSingleNode("City")).InnerText; 
                Location="国家名称:"+CountryName+"
区域名称:"+RegionName+"
城市名称:"+City; 
                Console.WriteLine(Location); 
            } 
            Console.Read(); 
        } 
    } 
}

文章主要通过GetElementsByTagName和SelectSingleNode两种方法获取Xml文档中提取标签Tag,获取http://www.freegeoip.net/xml/www.baidu.com中的物理位置.推荐大家学习C#中读取Xml文件制定结点的知识.运行结果如下图所示:

                 C# 网络编程之通过ip地址获取地理位置(补充)

下面提供一些这方面的有些非常优秀的内容仅供大家学习,同时也方便自己下次查阅:
1.C# 网络编程之获取本机名IP地址域名物理位置 (这是我自己的文章,结合该篇文章学习)
http://blog.csdn.net/eastmount/article/details/9270221
2.Get user location by IP address(使用C#获取物理位置 也是该篇文章的基础)
http://www.codeproject.com/Questions/686644/Get-user-location-by-IP-address
3.获取电脑物理地址以及通过IP地址获取当前地理位置的接口-抢街饭的专栏(使用PHP获取非常优秀的文章)
http://blog.csdn.net/lzwjavaphp/article/details/6972667
4.IP Address Location In ASP.NET(consume a free online API and fetch data using LINQ To XML)
http://www.dotnetcurry.com/showarticle.aspx?ID=325
5.get uesr location by ip address(关于它的讨论 希望大家去Stack Overflow学习知识)
http://*.com/questions/4327629/get-user-location-by-ip-address
6.XmlDocument.Load(url)问题(读取天气信息文章,与我的方法相同)
http://www.cnblogs.com/sharpfeng/archive/2011/03/02/1968666.html
最后希望该篇文章对大家有所帮助,同时希望该文章能帮助那位同学解决问题.如果文章中错误或不足之处,见谅!由于只会采用这种通过第三方接口的方法获取物理位置,很大程度依赖与第三方的数据,如果该网址被废除,很麻烦,同时会遇到不同网址查询结果也不同.希望有好的方法可以讨论分享.
(By:Eastmount 2014-1-22 下午5点 http://blog.csdn.net/eastmount)