【发布时间】:2010-08-18 19:15:27
【问题描述】:
我正在尝试从 Google 的地理编码 API 中解析出一些信息,但在有效地从 xml 中获取数据时遇到了一些麻烦。 See link for example
我真正关心的是从address_component 获取short_name,其中类型是administrative_area_level_1,从administrative_area_level_2 获取long_name
但是,在我的测试程序中,我的 XPath 查询对这两个查询都没有返回任何结果。
public static void Main(string[] args)
{
using(WebClient webclient = new WebClient())
{
webclient.Proxy = null;
string locationXml = webclient.DownloadString("http://maps.google.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
using(var reader = new StringReader(locationXml))
{
var doc = new XPathDocument(reader);
var nav = doc.CreateNavigator();
Console.WriteLine(nav.SelectSingleNode("/GeocodeResponse/result/address_component[type=administrative_area_level_1]/short_name").InnerXml);
Console.WriteLine(nav.SelectSingleNode("/GeocodeResponse/result/address_component[type=administrative_area_level_2]/long_name").InnerXml);
}
}
}
谁能帮我找出我做错了什么,或者推荐一个更好的方法?
【问题讨论】:
标签: c# xml xpath google-geocoder