我正在尝试从 Google 的地理编码 API 中解析出一些信息,但是在有效地从 xml 中获取数据时遇到了一些麻烦。例如见链接
我真正关心的是获取类型所在的位置和short_name
来自的位置,
但是使用我的测试程序,我的 XPath 查询对这两个查询都没有返回任何结果。address_component
administrative_area_level_1
long_name
administrative_area_level_2
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);
}
}
}
谁能帮我找出我做错了什么,或者推荐一个更好的方法?