2012-05-20 4 views

답변

4

물론 실제로 수행하려는 검색에 따라 다릅니다. 영국에서 Lon으로 시작하는 모든 위치를 찾고 싶다고 가정 해 보겠습니다. (많은 실제 검색을 위해 변경 될 수 있습니다, 예를 들어)이 검색을 수행 할 URL은 다음과 같습니다

http://api.geonames.org/search?name_startsWith=lon&country=GB&maxRows=10&username=demo 

당신은 당신의 브라우저에서 그 팝업 및 결과를 볼 수 있습니다

<geonames style="MEDIUM"> 
<totalResultsCount>334</totalResultsCount> 
<geoname> 
    <toponymName>London</toponymName> 
    <name>London</name> 
    <lat>51.50853</lat> 
    <lng>-0.12574</lng> 
    <geonameId>2643743</geonameId> 
    <countryCode>GB</countryCode> 
    <countryName>United Kingdom</countryName> 
    <fcl>P</fcl> 
    <fcode>PPLC</fcode> 
</geoname> 
<geoname> 
    <toponymName>Lone</toponymName> 
    <name>Lone</name> 
    <lat>58.33333</lat> 
    <lng>-4.88333</lng> 
    <geonameId>2643732</geonameId> 
    <countryCode>GB</countryCode> 
    <countryName>United Kingdom</countryName> 
    <fcl>P</fcl> 
    <fcode>PPL</fcode> 
</geoname> 
<!-- and so on ... --> 
</geonames> 

주를이 각 geoname 아래에 latlng 개의 요소가 필요합니다.

var xml = XElement.Load("http://api.geonames.org/search?name_startsWith=lon&country=GB&maxRows=10&username=demo"); 

var locations = xml.Descendants("geoname").Select(g => new { 
        Name = g.Element("name").Value, 
        Lat = g.Element("lat").Value, 
        Long = g.Element("lng").Value 
       }); 

foreach (var location in locations) 
{ 
    Console.WriteLine("{0}: {1}, {2}", location.Name, location.Lat, location.Long); 
} 

물론 다르게이 값을 사용하도록 선택할 수 있습니다, 당신은 두 배에 LatLong을 구문 분석 할 수 있습니다 : XML에 LINQ와 (네임 스페이스 선언에 System.LinqSystem.Linq.Xml 포함).

+0

그랬습니다. 고맙습니다! – Megaoctane

관련 문제