从以下 GeoCodeRespose xml 我们如何在 ac# 程序中使用 xpath 提取位置、路线和街道编号的值。
<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>
</result>
</GeocodeResponse>
到目前为止,我可以在 xml 文档中获取 xml,并且可以获取格式化地址的值,如下所示
XmlDocument doc=GetGeoCodeXMLResponse();
XPathDocument document = new XPathDocument(new XmlNodeReader(doc));
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator resultIterator = navigator.Select("/GeocodeResponse/result");
while (resultIterator.MoveNext())
{
XPathNodeIterator formattedAddressIterator = resultIterator.Current.Select("formatted_address");
while (formattedAddressIterator.MoveNext())
{
string FullAddress = formattedAddressIterator.Current.Value;
}
}