我想在下面的XML的<lat>
和<lng>
標籤之間的數據:獲取一個XML文件標籤之間的數據
<viewport>
<southwest>
<lat>41.7450495</lat>
<lng>-87.8859170</lng>
</southwest>
</viewport>
這是一個更大的XML的一部分:
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Chicago, IL, USA</formatted_address>
<address_component>
<long_name>Chicago</long_name>
<short_name>Chicago</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Cook</long_name>
<short_name>Cook</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Illinois</long_name>
<short_name>IL</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>
<geometry>
<location>
<lat>41.8781136</lat>
<lng>-87.6297982</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>41.7450495</lat>
<lng>-87.8859170</lng>
</southwest>
<northeast>
<lat>42.0109012</lat>
<lng>-87.3736794</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>41.6443350</lat>
<lng>-87.9402669</lng>
</southwest>
<northeast>
<lat>42.0231310</lat>
<lng>-87.5236609</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
現在,我想利用這個代碼來分析它:
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address="+address+"&sensor=false";
WebClient client = new WebClient();
string result = client.DownloadString(url);
var element = XElement.Parse(result);
var lat = (double)element.Element("lat");
var lng = (double)element.Element("lng");
但它不工作,元素是null
。我怎樣才能做到這一點?
強制性回答這類問題:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – 2011-06-07 05:31:33
爲什麼你想用正則表達式來讀取看起來是格式良好的XML? – 2011-06-07 05:32:59
你確定你不想要更多的東西如何解析XML文件?像http://stackoverflow.com/questions/55828/best-practices-to-parse-xml-files? – baraboom 2011-06-07 05:34:19