0
從以下GeoCodeRespose xml中,我們如何在c#程序中使用xpath提取地點,路線和街道號的值。如何使用xpaths在c#中提取xml節點
<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;
}
}
任何你不想使用XmlSerializer或LinqtoXml的原因? –
我打開使用XmlSerializer或LinqtoXml,如果你能給我一個例子來從xml中提取這些類型的值 – rumi
這個XML是否每次需要反序列化時都一致? –