获取xml文件中标签之间的数据
我想获取以下 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
。我该怎么做?
I want to get the data between the <lat>
and <lng>
tags in the following XML:
<viewport>
<southwest>
<lat>41.7450495</lat>
<lng>-87.8859170</lng>
</southwest>
</viewport>
which is part of a bigger 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>
Right now, I'm trying to use this code to parse it:
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");
but it isn't working, element is null
. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用正则表达式,解析它。这使用 LINQ to XML。
这应该适合你:
Don't use a regular expression, parse it. This uses LINQ to XML.
This should work for you:
与 DOTALL (s) 标志一起使用
,捕获组返回纬度和经度。
Use
with DOTALL (s) flag and the capturing groups return the latitude and longitude.
下面的代码工作正常......
The below code works fine....