10101
10101

Reputation: 2402

Read Open Street Map xml response

Trying to read data from Open Street Map webpage. I have Longitude and Latitude. Based on these web link is generated.

Let's say output is: https://nominatim.openstreetmap.org/reverse?format=xml&lat=48.927834&lon=16.861641&zoom=18&addressdetails=1

How to read webpage content? I have tried this but I am not getting anything in 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();

Getting:

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

enter image description here

My target is to parse 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;

Upvotes: 0

Views: 348

Answers (1)

Steeeve
Steeeve

Reputation: 915

You'll have to provide a valid user-agent in your requests, otherwise the requests will be blocked. From the current 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"

Upvotes: 1

Related Questions