2016-11-28 55 views
0

我正在向GoogleMaps API發出一個http請求,下面列出了我找回的XML。解析Google地圖GeocodeResponse XML並提取值

我想分配LAT和LON值到一個字符串,但我得到一個「空引用異常」對象引用未設置爲對象的實例。上線「串LAT = childnode ...

我根據我在這個question/answer

嘗試當我看到在節點列表中的所有調試器從XML的細節都在結果視圖。

任何想法我做錯了嗎?

這是問題的代碼。

// Parse the XML 
XmlDocument doc = new XmlDocument(); 
doc.LoadXml(responseFromServer); 

XmlNodeList nodeList = doc.SelectNodes("/*[local-name() = 'GeocodeResponse']/*[local-name() = 'result']"); 

if (nodeList != null) 
{ 
    foreach (XmlNode childNode in nodeList) 
    { 
     string lat = childNode.SelectSingleNode("//*[local-name() = 'geometry/location/lat']").InnerText; 
     string lon = childNode.SelectSingleNode("//*[local-name() = 'geometry/location/lon']").InnerText; 
     string locationType = childNode.SelectSingleNode("//*[local-name() = 'geometry/location_type']").InnerText; 
    } 
} 

這裏是XML的一個例子是谷歌地圖懲戒甕

地址更改爲保護無辜者:)

<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<GeocodeResponse> 
<status>OK</status> 
<result> 
    <type>street_address</type> 
    <formatted_address>48 Johnson Ave, Wiley Park NSW 2195, Australia</formatted_address> 
    <address_component> 
     <long_name>48</long_name> 
     <short_name>48</short_name> 
     <type>street_number</type> 
    </address_component> 
    <address_component> 
     <long_name>Johnson Avenue</long_name> 
     <short_name>Johnson Ave</short_name> 
     <type>route</type> 
     </address_component> 
    <address_component> 
     <long_name>Wiley Park</long_name> 
     <short_name>Wiley Park</short_name> 
     <type>locality</type> 
     <type>political</type> 
    </address_component> 
    <address_component> 
     <long_name>Canterbury City Council</long_name> 
     <short_name>Canterbury</short_name> 
     <type>administrative_area_level_2</type> 
     <type>political</type> 
    </address_component> 
    <address_component> 
     <long_name>New South Wales</long_name> 
     <short_name>NSW</short_name> 
     <type>administrative_area_level_1</type> 
     <type>political</type> 
    </address_component> 
    <address_component> 
     <long_name>Australia</long_name> 
     <short_name>AU</short_name> 
     <type>country</type> 
     <type>political</type> 
    </address_component> 
    <address_component> 
     <long_name>2195</long_name> 
     <short_name>2195</short_name> 
     <type>postal_code</type> 
    </address_component> 
    <geometry> 
     <location> 
      <lat>-33.9279554</lat> 
      <lng>151.0688625</lng> 
     </location> 
     <location_type>ROOFTOP</location_type> 
     <viewport> 
      <southwest> 
       <lat>-33.9293044</lat> 
       <lng>151.0675135</lng> 
      </southwest> 
      <northeast> 
       <lat>-33.9266064</lat> 
       <lng>151.0702115</lng> 
      </northeast> 
     </viewport> 
    </geometry> 
    <partial_match>true</partial_match> 
     <place_id>ChIJkUHpDOe7EmsRGapwFB6s9Dw</place_id> 
    </result> 
</GeocodeResponse> 

編輯: 它正在對我來說,我還沒有指定的路徑,我想正確的節點。 不知道我應該改變什麼。

回答

0

使用更簡單的XPath電話:

  XmlDocument doc = new XmlDocument(); 
      doc.Load("google.xml"); 

      XmlNodeList nodeList = doc.SelectNodes("/*[local-name() = 'GeocodeResponse']/*[local-name() = 'result']"); 

      if (nodeList != null) 
      { 
       foreach (XmlNode childNode in nodeList) 
       { 
        string lat = childNode.SelectSingleNode("geometry/location/lat").InnerText; 
        string lon = childNode.SelectSingleNode("geometry/location/lng").InnerText; 
        string locationType = childNode.SelectSingleNode("geometry/location_type").InnerText; 
       } 
      } 

也...經度節點是 'LGN' 不LON

+0

完美:)我已經spoted LON的LNG問題,不過這/ * [本地姓名的東西只是讓我困惑。你所擁有的更有意義,就像我已經在聲明中指出的那樣。現在我必須嘗試挑出市議會名稱,但我會盡力爲自己弄清楚。感謝你的協助。 –