2013-02-07 22 views
0

嘿,我的想法很簡單,我會從Utrace API的IP地址(例如代理服務器)獲取位置信息。使用Utrace獲取位置後解析XML

看看這段代碼(感謝谷歌)

public static string GetLocation(string IP) 
    { 
     var location = ""; 
     List<string> HTML_code = new List<string>(); 
     WebRequest request = WebRequest.Create("http://xml.utrace.de/?query=" + IP); 
     using (WebResponse response = request.GetResponse()) 
     using (StreamReader stream = new StreamReader(response.GetResponseStream())) 
     { 
      string line; 
      while ((line = stream.ReadLine()) != null) 
      { 
       HTML_code.Add(line); 
      } 
     } 
     location = (HTML_code[296].Replace("<td><font size=\"-1\">", "")).Replace("</font></td>", ""); 
     return location; 
    } 

OK但是從utrace API輸出如下:

<?xml version="1.0" encoding="iso-8859-1"?> 
    <results> 
    <result> 
    <ip>188.40.16.134</ip> 
    <host></host> 
    <isp>Hetzner Online AG Pagedesign</isp> 
    <org>Hetzner Online AG Pagedesign</org> 
    <region>Koblenz</region> 
    <countrycode>DE</countrycode> 
    <latitude>50.349998474121</latitude> 
    <longitude>7.5999999046326</longitude> 
    <queries>8</queries> 
    </result> 
    </results> 

我的XML技能是不是最好的,我希望你們能幫我編輯這行:

location = (HTML_code[296].Replace("<td><font size=\"-1\">", "")).Replace("</font></td>", ""); 

我會喜歡這樣的輸出:

Hetzner Online AG Pagedesign : Koblenz 

,而不是

<?xml version="1.0" encoding="iso-8859-1"?> 
    <results> 
    <result> 
    <ip>188.40.16.134</ip> 
    <host></host> 
    <isp>Hetzner Online AG Pagedesign</isp> 
    <org>Hetzner Online AG Pagedesign</org> 
    <region>Koblenz</region> 
    <countrycode>DE</countrycode> 
    <latitude>50.349998474121</latitude> 
    <longitude>7.5999999046326</longitude> 
    <queries>8</queries> 
    </result> 
    </results> 

在此先感謝您的幫助

編輯:

我的新代碼如下:

public static void getloc(string ip) 
    { 
     var location = ""; 
     var wc = new WebClient(); 
     location = wc.DownloadString("http://xml.utrace.de/?query=" + ip); 
     location = location.Replace("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>", "").Replace("<results>", "").Replace("<result>", "") 
      .Replace("<ip></ip>", "").Replace("<org></org>", "").Replace("<latitude></latitude>", "").Replace("<longitude></longitude>", "").Replace("<queries>*</queries>", "") 
      .Replace("</result>", "").Replace("</results>", ""); 
     Console.WriteLine(location); 
    } 

輸出則爲:

<ip>212.19.62.76</ip> 
<host>1</host> 
<isp>Plus.line AG</isp> 
<org>ANW GmbH &amp; Co. KG</org> 
<region>Bechhofen</region> 
<countrycode>DE</countrycode> 
<latitude>49.150001525879</latitude> 
<longitude>10.550000190735</longitude> 
<queries>6</queries> 

我怎樣才能像 Plus.line AG ANW有限公司& Co. KG的貝肖芬

問候和感謝

回答

0

加載XML輸出到XmlDocument並正確解析它。

public static void getLoc(string ip) 
{ 
    var wc = new WebClient(); 
    string location = wc.DownloadString("http://xml.utrace.de/?query=" + ip); 

    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(location); 

    XmlNode orgNode = doc.SelectSingleNode("//org/text()"); 
    XmlNode regionNode = doc.SelectSingleNode("//region/text()"); 

    return String.Format("{0} {1}", orgNode.Value, regionNode.Value); 
} 
+0

非常感謝你非常感謝你這是一個奇妙的解決方案,謝謝 –

0

這是非常簡單的使用LINQ到XML:

XDocument xdoc = XDocument.Parse(xml); 
var result = xdoc.Descendatns("result") 
       .Select(r => new { 
        Org = (string)r.Element("org"), 
        Region = (string)r.Element("region") 
       }).Single(); 

這將返回強類型的匿名對象與性質組織和地區。格式:

String.Format("{0} : {1}", result.Org, result.Region); 
+1

是的,謝謝你。再次我已經學到了一點:) –

+0

如果你打電話給'選擇'方法調用「簡單」,你一直在喝太多的庫爾 - 援助。 –

+0

@JonGrant select是查詢的一部分,如果你知道C#,這很簡單。而這個特別的選擇非常簡單。我相信大多數C#程序員會發現C++指針是複雜的。但對於C++程序員來說,這是一件很平常的事情。 –