2013-10-13 65 views
0

使用下面我们如何纬度和经度使用LINQ的值提取到XML拉姆达XML以提取XML节点查询如何使用LinqtoXml

<?xml version="1.0" encoding="UTF-8"?> 
<GeocodeResponse> 
<status>OK</status> 
<result> 
    <type>premise</type> 
    <formatted_address>Livingston, West Lothian EH54, UK</formatted_address> 
    <address_component> 
     <long_name>United Kingdom</long_name> 
     <short_name>GB</short_name> 
     <type>country</type> 
     <type>political</type> 
    </address_component> 
    <geometry> 
     <location> 
      <lat>55.8831307</lat> 
      <lng>-3.5162945</lng> 
     </location> 
     <location_type>APPROXIMATE</location_type> 
    </geometry> 
    <partial_match>true</partial_match> 
</result> 

我米使用这样的事情来提取其他节点

var xDoc = XDocument.Parse(doc.OuterXml); 

     model = xDoc.Descendants("result").Select(c => 
        new 
        { 
         FullAddress = c.Element("formatted_address").Value, 
         CountryName = c.Descendants("address_component") 
         .First(e => e.Element("type").Value == "country").Element("long_name").Value, 
         }).First(); 
+0

它知道一些ID或相关的身份,以便获取'是info'使用是很重要的。它也**决定**我们如何通过'LINQ-to-XML'获取信息 –

回答

2
XDocument doc=XDocument.Load(url); 
var location=doc.Descendants("location") 
       .Select(x=> 
        new 
        { 
          lat=x.Element("lat").Value, 
          lng=x.Element("lng").Value 
        }).First(); 

location.lat; 
location.lng;