如何使用 c# 解析 XML? [英] How to parse XML using c#?

查看:20
本文介绍了如何使用 c# 解析 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用谷歌的地理编码 API,但它返回 XML,我不知道如何解析它.

I'm using google's Geocoding API but it return XML and I do not know how to parse it.

我正在尝试获取看起来像这样的 XML 文档的纬度...

I'm trying to get the latitude in the XML document that looks like this...

<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>street_address</type>
  <formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address>
  <address_component>
   <long_name>1600</long_name>
   <short_name>1600</short_name>
   <type>street_number</type>
  </address_component>
  <address_component>
   <long_name>Amphitheatre Pkwy</long_name>
   <short_name>Amphitheatre Pkwy</short_name>
   <type>route</type>
  </address_component>
  <address_component>
   <long_name>Mountain View</long_name>
   <short_name>Mountain View</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>San Jose</long_name>
   <short_name>San Jose</short_name>
   <type>administrative_area_level_3</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Santa Clara</long_name>
   <short_name>Santa Clara</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>California</long_name>
   <short_name>CA</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>94043</long_name>
   <short_name>94043</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>37.4217550</lat>
    <lng>-122.0846330</lng>
   </location>
   <location_type>ROOFTOP</location_type>
   <viewport>
    <southwest>
     <lat>37.4188514</lat>
     <lng>-122.0874526</lng>
    </southwest>
    <northeast>
     <lat>37.4251466</lat>
     <lng>-122.0811574</lng>
    </northeast>
   </viewport>
  </geometry>
 </result>
</GeocodeResponse>

我该怎么做?这是我能做到的.

How can I do that? This is as far as I could get to.

string baseURL = "https://maps.googleapis.com/maps/api/geocode/xml?";
string fullURL = baseURL  + "address=" + address + "&sensor=false";
Uri ServivrUri = new Uri(fullURL);
WebClient proxy = new WebClient();
byte[] abc = proxy.DownloadData(ServivrUri);

MemoryStream stream = new MemoryStream(abc);
XmlDocument xDocument = new XmlDocument();
xDocument.Load(stream);

推荐答案

你应该使用 XDocument 而不是 XmlDocument

You should use XDocument instead of XmlDocument

var xDocument = XDocument.Load(stream);

然后就可以使用linqToXml

Then you can use linqToXml

比如获取纬度,你可以试试这个

For instance, for getting the latitude, you can try this

var latitude = xDocument.Descendants("geometry").Descendants("location")
                        .First().Element("lat").Value;

这篇关于如何使用 c# 解析 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆