使用Utrace获取位置后正在分析XML
本文关键字:XML Utrace 获取 位置 使用 | 更新日期: 2023-09-27 17:58:14
嘿,伙计们,我的想法很简单,我将使用Utrace API从IP地址(例如代理)获取位置信息。
看看这个代码(感谢谷歌)
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;
}
好的,但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 & Co. KG</org>
<region>Bechhofen</region>
<countrycode>DE</countrycode>
<latitude>49.150001525879</latitude>
<longitude>10.550000190735</longitude>
<queries>6</queries>
我怎样才能得到这样的输出Plus.line AG ANW GmbH;Co.KG Bechhofen
问候并感谢
将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);
}
使用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();
这将返回具有属性Org和Region的强类型匿名对象。格式:
String.Format("{0} : {1}", result.Org, result.Region);