2013-07-17 182 views
0

我試圖從谷歌網址獲取一個叫lat的元素。一切正常,當我得到的XML文件並加載它,但我無法從拉元素獲取內容並將其存儲在一個變量(最好是字符串,但不限於)。顯然我想爲lng元素做同樣的事情。有人能幫助我嗎? Thx,非常感謝。c#和xml元素查詢

我的代碼:

var url = String.Format("http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=false", Server.UrlEncode(Model.UserQuery)); 
XElement GoogFile = XElement.Load(url); 
//Everything above works 
var Lat = GoogFile.Element("lat"); //This returns null in the variable 
var Lng = GoogFile.Element("lng"); 

XML文件(我想從哪個是geometry-location.It下,該文件的緯度和經度是這樣的:44.200797024.5022981

<?xml version="1.0" encoding="UTF-8" ?> 
- <GeocodeResponse> 
    <status>OK</status> 
- <result> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    <formatted_address>Olt County, Romania</formatted_address> 
- <address_component> 
    <long_name>Olt County</long_name> 
    <short_name>OT</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Romania</long_name> 
    <short_name>RO</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>44.2007970</lat> 
    <lng>24.5022981</lng> 
    </location> 
    <location_type>APPROXIMATE</location_type> 
- <viewport> 
- <southwest> 
    <lat>43.6897579</lat> 
    <lng>23.8703090</lng> 
    </southwest> 
- <northeast> 
    <lat>44.9093370</lat> 
    <lng>24.8995099</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>43.6897579</lat> 
    <lng>23.8703090</lng> 
    </southwest> 
- <northeast> 
    <lat>44.9093370</lat> 
    <lng>24.8995099</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>Vía 40, Barranquilla, Atlantico, Colombia</formatted_address> 
- <address_component> 
    <long_name>Vía 40</long_name> 
    <short_name>0</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Barranquilla</long_name> 
    <short_name>Barranquilla D.E.I y P.</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Atlantico</long_name> 
    <short_name>Atlantico</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Colombia</long_name> 
    <short_name>CO</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>11.0170300</lat> 
    <lng>-74.7961300</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>10.9854500</lat> 
    <lng>-74.8266400</lng> 
    </southwest> 
- <northeast> 
    <lat>11.0393100</lat> 
    <lng>-74.7759300</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>10.9854500</lat> 
    <lng>-74.8266400</lng> 
    </southwest> 
- <northeast> 
    <lat>11.0393100</lat> 
    <lng>-74.7759300</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>Northeast 480, Andrews, TX 79714, USA</formatted_address> 
- <address_component> 
    <long_name>Northeast 480</long_name> 
    <short_name>NE 480</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Andrews</long_name> 
    <short_name>Andrews</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Andrews</long_name> 
    <short_name>Andrews</short_name> 
    <type>administrative_area_level_2</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Texas</long_name> 
    <short_name>TX</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>79714</long_name> 
    <short_name>79714</short_name> 
    <type>postal_code</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>32.3283355</lat> 
    <lng>-102.4862856</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>32.3258400</lat> 
    <lng>-102.4867120</lng> 
    </southwest> 
- <northeast> 
    <lat>32.3305689</lat> 
    <lng>-102.4774341</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>32.3258400</lat> 
    <lng>-102.4867120</lng> 
    </southwest> 
- <northeast> 
    <lat>32.3305689</lat> 
    <lng>-102.4774341</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>0, Cottonwood, IL 62440, USA</formatted_address> 
- <address_component> 
    <long_name>0</long_name> 
    <short_name>0</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Cottonwood</long_name> 
    <short_name>Cottonwood</short_name> 
    <type>administrative_area_level_3</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Cumberland</long_name> 
    <short_name>Cumberland</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> 
- <address_component> 
    <long_name>62440</long_name> 
    <short_name>62440</short_name> 
    <type>postal_code</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>39.3603100</lat> 
    <lng>-88.2434670</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>39.3457376</lat> 
    <lng>-88.2447235</lng> 
    </southwest> 
- <northeast> 
    <lat>39.3603100</lat> 
    <lng>-88.2420255</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>39.3457376</lat> 
    <lng>-88.2434670</lng> 
    </southwest> 
- <northeast> 
    <lat>39.3603100</lat> 
    <lng>-88.2432820</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>0, Cheyenne, WY 82009, USA</formatted_address> 
- <address_component> 
    <long_name>0</long_name> 
    <short_name>0</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Cheyenne</long_name> 
    <short_name>Cheyenne</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Laramie</long_name> 
    <short_name>Laramie</short_name> 
    <type>administrative_area_level_2</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Wyoming</long_name> 
    <short_name>WY</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>82009</long_name> 
    <short_name>82009</short_name> 
    <type>postal_code</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>41.2555110</lat> 
    <lng>-104.5239096</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>41.2537342</lat> 
    <lng>-104.5249831</lng> 
    </southwest> 
- <northeast> 
    <lat>41.2573833</lat> 
    <lng>-104.5221876</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>41.2537342</lat> 
    <lng>-104.5249831</lng> 
    </southwest> 
- <northeast> 
    <lat>41.2573833</lat> 
    <lng>-104.5221876</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>0, Bessemer City, NC 28016, USA</formatted_address> 
- <address_component> 
    <long_name>0</long_name> 
    <short_name>0</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Bessemer City</long_name> 
    <short_name>Bessemer City</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Crowders Mountain</long_name> 
    <short_name>Crowders Mountain</short_name> 
    <type>administrative_area_level_3</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Gaston</long_name> 
    <short_name>Gaston</short_name> 
    <type>administrative_area_level_2</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>North Carolina</long_name> 
    <short_name>NC</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>28016</long_name> 
    <short_name>28016</short_name> 
    <type>postal_code</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>35.2801489</lat> 
    <lng>-81.3082181</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>35.2785860</lat> 
    <lng>-81.3095352</lng> 
    </southwest> 
- <northeast> 
    <lat>35.2812839</lat> 
    <lng>-81.3068372</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>35.2795895</lat> 
    <lng>-81.3089594</lng> 
    </southwest> 
- <northeast> 
    <lat>35.2802804</lat> 
    <lng>-81.3074130</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>Zero Street, Sana'a, Yemen</formatted_address> 
- <address_component> 
    <long_name>Zero Street</long_name> 
    <short_name>0</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Sana'a</long_name> 
    <short_name>Sana'a</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Amanat Al Asimah</long_name> 
    <short_name>Amanat Al Asimah</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Yemen</long_name> 
    <short_name>YE</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>15.3021640</lat> 
    <lng>44.1953690</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>15.2972988</lat> 
    <lng>44.1938378</lng> 
    </southwest> 
- <northeast> 
    <lat>15.3060185</lat> 
    <lng>44.1970439</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>15.2972988</lat> 
    <lng>44.1938378</lng> 
    </southwest> 
- <northeast> 
    <lat>15.3060185</lat> 
    <lng>44.1970439</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>0, Sana'a, Yemen</formatted_address> 
- <address_component> 
    <long_name>0</long_name> 
    <short_name>0</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Sana'a</long_name> 
    <short_name>Sana'a</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Amanat Al Asimah</long_name> 
    <short_name>Amanat Al Asimah</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Yemen</long_name> 
    <short_name>YE</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>15.3021640</lat> 
    <lng>44.1953690</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>15.2973087</lat> 
    <lng>44.1939675</lng> 
    </southwest> 
- <northeast> 
    <lat>15.3059751</lat> 
    <lng>44.1970439</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>15.2973087</lat> 
    <lng>44.1939675</lng> 
    </southwest> 
- <northeast> 
    <lat>15.3059751</lat> 
    <lng>44.1970439</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
- <result> 
    <type>route</type> 
    <formatted_address>Río Fuerte, Juan José Ríos, General Juan José Ríos, Sinaloa, Mexico</formatted_address> 
- <address_component> 
    <long_name>Río Fuerte</long_name> 
    <short_name>Río Fuerte</short_name> 
    <type>route</type> 
    </address_component> 
- <address_component> 
    <long_name>Juan José Ríos</long_name> 
    <short_name>Juan José Ríos</short_name> 
    <type>sublocality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>General Juan José Ríos</long_name> 
    <short_name>Gral Juan José Ríos</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Sinaloa</long_name> 
    <short_name>SIN</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
- <address_component> 
    <long_name>Mexico</long_name> 
    <short_name>MX</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
- <geometry> 
- <location> 
    <lat>25.7610486</lat> 
    <lng>-108.8163917</lng> 
    </location> 
    <location_type>GEOMETRIC_CENTER</location_type> 
- <viewport> 
- <southwest> 
    <lat>25.7553290</lat> 
    <lng>-108.8275377</lng> 
    </southwest> 
- <northeast> 
    <lat>25.7668749</lat> 
    <lng>-108.8059156</lng> 
    </northeast> 
    </viewport> 
- <bounds> 
- <southwest> 
    <lat>25.7553290</lat> 
    <lng>-108.8275377</lng> 
    </southwest> 
- <northeast> 
    <lat>25.7668749</lat> 
    <lng>-108.8059156</lng> 
    </northeast> 
    </bounds> 
    </geometry> 
    </result> 
    </GeocodeResponse> 

回答

7

。你在文件中要求直接的元素 - 而大概要在location元素的那些因此,像這樣:

var location = GoogFile.Descendants("location").First(); 
var lat = location.Element("lat"); 
var lng = location.Element("lng"); 

(這是假設你真的想要的location元素 - 如果你想要一個不同的一個,無論是適應上面的代碼或更精確。)

+0

我覺得你更接近要現實。 –

+0

Thx,回答所有人。這工作,但我需要將其轉換爲int是可能的,因爲我需要擺脫元素標籤。我只想要內容。 – user2138160

+0

nvm。我想到了。我只需要將.value添加到lat和lng。 Thx爲您提供幫助!你爲我節省了很多時間。 – user2138160

0

不是GoogFile的直接後代,這就是Element(「lat」)返回null的原因。

您可以使用它代替:

var Lat = GoogFile.Descendants("lat").FirstOrDefault(); 
0
XmlDocument xml = new XmlDocument(); 
xml.Load(filename); 

// then use xpath 
string lat = xml.SelectSingleNode("/GeocodeResponse/geometry/location/lat").InnerText; 
string lng = xml.SelectSingleNode("/GeocodeResponse/geometry/location/lng").InnerText;