阅读开放街道地图 xml 回复

Read Open Street Map xml response

正在尝试从 Open Street Map 网页读取数据。我有经度和纬度。基于这些网站 link 生成。

假设输出是: https://nominatim.openstreetmap.org/reverse?format=xml&lat=48.927834&lon=16.861641&zoom=18&addressdetails=1

如何阅读网页内容?我已经试过了,但我在 result.

中没有得到任何东西
  string urlString = "https://nominatim.openstreetmap.org/reverse?format=xml&lat=48.927834&lon=16.861641&zoom=18&addressdetails=1";

  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urlString);
  myRequest.Method = "GET";
  WebResponse myResponse = myRequest.GetResponse();
  StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
  string result = sr.ReadToEnd();
  sr.Close();
  myResponse.Close();

获得:

Exception thrown: 'System.Net.WebException' in System.Net.Requests.dll Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll

我的目标是解析 xml:

  // Loading from a file, you can also load from a stream
  XDocument xml = XDocument.Parse(result);

  // Query the data and write out a subset of contacts
  IEnumerable<string> query = from c in xml.Root.Descendants("addressparts")
              select c.Element("road").Value + " " +
                     c.Element("house_number").Value;

您必须在请求中提供有效的用户代理,否则请求将被阻止。从当前 Nominatim Usage Policy:

  • Provide a valid HTTP Referer or User-Agent identifying the application (stock User-Agents as set by http libraries will not do).
myRequest.UserAgent = "MyApp Version 1.2.3.4"