从asp.net中的http请求中检索XML的最佳方法?

时间:2012-03-06 15:47:58

标签: asp.net xml openstreetmap

我正在尝试使用我的asp.net应用程序中的openstreetmap地理编码服务进行地理编码。

OSM地理编码服务称为URL查询字符串。这是他们网站上的一个例子:

http://nominatim.openstreetmap.org/search?q=135+pilkington+avenue,+birmingham&format=xml&polygon=1&addressdetails=1

该地址返回XML。

我正在寻找最佳(最佳实践?)方法来检索XML数据。我的意思是:

Dim MyXMLStringResponse as string
dim MyParametricURL as string

MyParametricURL="http://nominatim.openstreetmap.org/search?q=" & myaddress & "&format=xml&polygon=1&addressdetails=1"

MyXMLStringResponse = SomeObjectOrFunction.SomeMethod(MyParametricURL) 

我正在寻找的是“SomeObjectOrFunction”或“SomeMethod”来检索字符串XML。

以防万一; nominatim.openstreetmap.org不是网络服务。

1 个答案:

答案 0 :(得分:1)

一种简单的方法是使用WebClient类发送请求。

这是MSDN演示代码的修剪版本:

        WebClient client = new WebClient ();

        // Add a user agent header in case the 
        // requested URI contains a query.

        client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        Stream data = client.OpenRead ("http://nominatim...");
        StreamReader reader = new StreamReader (data);
        string xml = reader.ReadToEnd ();

        // "xml" now contains the response in string format
        // Do whatever (load in XmlDocument, for example)  

        // close connection  
        data.Close ();
        reader.Close ();
相关问题