【发布时间】:2021-11-28 17:54:48
【问题描述】:
尝试从 Open Street Map 网页读取数据。我有经度和纬度。基于这些网页链接生成。
如何阅读网页内容?我已经尝试过了,但在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();
获取:
抛出异常:System.Net.Requests.dll 中的“System.Net.WebException” 抛出异常:“System.Net.WebException”在 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;
【问题讨论】:
-
异常详情是什么?
-
你写的是你得到了一个 WebException。
-
好吧,异常被抛出,但你没有捕捉到它,方法以那个异常结束。在方法中加入 try/catch 或在 Visual Studio 的异常设置窗口中检查所有 CLR 异常类型。
-
@Steeeve 添加了详细信息
-
@Steeeve 发布的解决方案似乎有效。也许您可以将其添加为答案
标签: c# xml xml-parsing xmlhttprequest openstreetmap