开发者

linq to XML string

I got like answer from WebClient String (xml). How to extract with Linq some attributes, for example lat and lng ?

<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>route</type>
  <formatted_address>Prince Michael St, Belgrade, Serbia</formatted_address>
  <address_component>
   <long_name>Prince Michael St</long_name>
   <short_name>Prince Michael St</short_name>
   <type>route</type>
  </address_component>
  <address_component>
   <long_name>Stari Grad</long_name>
   <short_name>Stari Grad</short_name>
   <type>sublocality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Belgrade</long_name>
   <short_name>Belgrade</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>City of Belgrade</long_name>
   <short_name>City of Belgrade</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Central Serbia</long_name>
   <short_name>Central Serbia</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Serbia</long_name>
   <short_name>RS</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>44.8157361</lat>
    <lng>20.4593997</lng>
   </location>
   <location_type>GEOMETRIC_CENTER</location_type>
   <viewport>
    <southwest>
     <lat>44.8122468</lat>
     <lng>20.4564820</lng>
    </southwest>
    <northeast>
     <lat>44.8185421</lat>
     <lng>20.4627772&开发者_开发百科lt;/lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>44.8150440</lat>
     <lng>20.4593895</lng>
    </southwest>
    <northeast>
     <lat>44.8157449</lat>
     <lng>20.4598697</lng>
    </northeast>
   </bounds>
  </geometry>
 </result>
</GeocodeResponse>


You can use XDocument.Parse method to get an XDocument object out of a string. You can apply LINQ to XML just like you'd do for any XDocument. For instance, you can get all <lat> elements in the document with:

XDocument.Parse(xmlString).Descendants("lat")

The exact query you use depends on what exactly you want to extract from the document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜