2013-10-14 3 views
1

Google의 Geocoding API를 사용하고 있지만 XML을 반환하기 때문에 구문 분석 방법을 모르겠습니다.C#을 사용하여 XML을 구문 분석하는 방법?

나는이처럼 보이는 XML 문서의 위도를 얻으려고 ...

<GeocodeResponse> 
<status>OK</status> 
<result> 
    <type>street_address</type> 
    <formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address> 
    <address_component> 
    <long_name>1600</long_name> 
    <short_name>1600</short_name> 
    <type>street_number</type> 
    </address_component> 
    <address_component> 
    <long_name>Amphitheatre Pkwy</long_name> 
    <short_name>Amphitheatre Pkwy</short_name> 
    <type>route</type> 
    </address_component> 
    <address_component> 
    <long_name>Mountain View</long_name> 
    <short_name>Mountain View</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>San Jose</long_name> 
    <short_name>San Jose</short_name> 
    <type>administrative_area_level_3</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>Santa Clara</long_name> 
    <short_name>Santa Clara</short_name> 
    <type>administrative_area_level_2</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>California</long_name> 
    <short_name>CA</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>United States</long_name> 
    <short_name>US</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>94043</long_name> 
    <short_name>94043</short_name> 
    <type>postal_code</type> 
    </address_component> 
    <geometry> 
    <location> 
    <lat>37.4217550</lat> 
    <lng>-122.0846330</lng> 
    </location> 
    <location_type>ROOFTOP</location_type> 
    <viewport> 
    <southwest> 
    <lat>37.4188514</lat> 
    <lng>-122.0874526</lng> 
    </southwest> 
    <northeast> 
    <lat>37.4251466</lat> 
    <lng>-122.0811574</lng> 
    </northeast> 
    </viewport> 
    </geometry> 
</result> 
</GeocodeResponse> 

내가 어떻게 할 수 있습니까? 이것은 내가 갈 수있는 한도 내입니다.

string baseURL = "https://maps.googleapis.com/maps/api/geocode/xml?"; 
string fullURL = baseURL + "address=" + address + "&sensor=false"; 
Uri ServivrUri = new Uri(fullURL); 
WebClient proxy = new WebClient(); 
byte[] abc = proxy.DownloadData(ServivrUri); 

MemoryStream stream = new MemoryStream(abc); 
XmlDocument xDocument = new XmlDocument(); 
xDocument.Load(stream); 
+4

해보십시오하여 XDocument를 시도 할 수 있습니다 , XmlDocument보다 사용하기가 쉽습니다. –

+0

XPath를 사용하십시오. 네비게이션은 SOO가 훨씬 쉽습니다. –

+0

@CoryNelson에서 몇 가지 예를들 수 있습니다. 나는이 일을하는 방법에 대해 웹 온통 보았지만 그냥 아무것도 함께 넣을 수없는 것. – wyatt

답변

2

당신은 그럼 당신은 예를 들어 linqToXml

을 사용할 수 있습니다 대신 XmlDocument

var xDocument = XDocument.Load(stream); 

XDocument를 사용해야합니다, 위도를 얻기 위해, 당신은이

var latitude = xDocument.Descendants("geometry").Descendants("location") 
         .First().Element("lat").Value; 
+0

VS에서 Descendants()에 대한 참조를 찾을 수 없습니다. System.Xml.Linq 네임 스페이스에 있습니까? @Esteban Elverdin – wyatt

+0

죄송합니다. XmlDocument를 사용하고 있다는 것을 눈치 채지 못했습니다. 대답을 업데이트했습니다. –

+0

그래, Cory의 의견에서 그걸 잡으려고했습니다. 이걸 시험해 볼께 고마워. – wyatt

관련 문제