0

我正在尝试从 google url 获取一个名为 lat 的元素。当我获取 xml 文件并加载它时一切正常,但是在我无法从 lat 元素获取内容并将其存储在变量中(最好是字符串,但不限于)之后。显然我想对 lng 元素做同样的事情。有人可以帮助我吗?谢谢,非常感谢。

我的代码:

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 文件(我想从几何位置下的文件中获取 lat 和 lng。它是: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>
4

4 回答 4

7

直接在文档中要求元素 - 而大概您想要元素中的location元素。所以是这样的:

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

(这是假设你真的想要这个location元素——如果你想要一个不同的元素,要么调整上面的代码,要么更精确。)

于 2013-07-17T20:28:14.033 回答
0

用 LINQ 试试:http: //msdn.microsoft.com/de-de/library/bb342765.aspx

IEnumerable<XElement> elements =
from el in GoogFile.Elements("lat")
select el;
foreach (XElement el in elements)
    Console.WriteLine(el);
于 2013-07-17T20:30:10.747 回答
0

不是 GoogFile 的直接后代,这就是 Element("lat") 返回 null 的原因。

您可以改用它:

var Lat = GoogFile.Descendants("lat").FirstOrDefault();
于 2013-07-17T20:36:38.673 回答
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;
于 2013-07-17T20:37:42.430 回答