[英]How to parse XML-Response in C# from Nokia-HERE Geocoding REST API
介紹
我正在用C#編寫WinForm應用程序,在其中我請求給定地址的地圖位置(緯度/經度)。
我使用諾基亞HERE地理編碼REST API並請求XML,然后嘗試對其進行解析。 以下API調用來自諾基亞文檔:
REST API呼叫
http://geocoder.cit.api.here.com/6.2/geocode.xml?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg&gen=8&searchtext=425+W+Randolph+Chicago
此API調用返回一個XML流,如下所示(簡短):
XML響應
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Search xmlns:ns2="http://www.navteq.com/lbsp/Search-Search/4">
<Response>
<MetaInfo>
<Timestamp>2014-12-15T10:11:29.197Z</Timestamp>
</MetaInfo>
<View xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SearchResultsViewType">
<ViewId>0</ViewId>
<Result>
<Relevance>1.0</Relevance>
<MatchLevel>houseNumber</MatchLevel>
<MatchQuality>
<City>1.0</City>
<Street>0.9</Street>
<HouseNumber>1.0</HouseNumber>
</MatchQuality>
<MatchType>pointAddress</MatchType>
<Location>
<LocationId>NT_krOz+rwboyk4Jvih55MwPB_425</LocationId>
<LocationType>address</LocationType>
<DisplayPosition>
<Latitude>41.8838692</Latitude>
<Longitude>-87.6389008</Longitude>
</DisplayPosition>
<NavigationPosition>
<Latitude>41.8844719</Latitude>
<Longitude>-87.6387711</Longitude>
</NavigationPosition>
</Location>
</Result>
</View>
</Response>
</ns2:Search>
因為我只對提取<NavigationPosition>
節點內的內容感興趣,所以我認為可以編寫以下代碼:
C#代碼片段檢索XML
var req = (HttpWebRequest)WebRequest.Create("http://geocoder.cit.api.here.com/6.2/geocode.xml?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg&gen=8&searchtext=425+W+Randolph+Chicago");
req.Method = "GET";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.ContentType = "application/x-www-form-urlencoded";
req.Timeout = 5000;
var resp = (HttpWebResponse)req.GetResponse();
var ms = new MemoryStream();
var rs = resp.GetResponseStream();
if (rs != null)
{
var buf = new byte[4096];
int len = 0;
while ((len = rs.Read(buf, 0, buf.Length)) > 0)
{
ms.Write(buf, 0, len);
}
rs.Close();
}
var xml = new XmlDocument();
xml.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
var status = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition");
if (status.Count == 1)
{
var lat = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Latitude");
var lng = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Longitude");
}
我必須了解到我無法以這種方式解析此特定XML,因為它包含名稱空間前綴(ns2)
。 因此,我添加了名稱空間管理器:
新增名稱空間管理員
XmlNamespaceManager nsmanager = new XmlNamespaceManager(xml.NameTable);
nsmanager.AddNamespace("ns2", "http://www.navteq.com/lbsp/Search-Search/4");
並將xpath查詢xml.SelectNodes()
更改為:
更新的C#代碼
var status = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition", nsmanager);
if (status.Count == 1)
{
var lat = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Latitude", nsmanager);
var lng = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Longitude", nsmanager);
}
我的問題
現在,添加了NS管理器之后,我就可以讀取和解析XML響應了。
由於我只對第一個結果的lat / lng值感興趣(可能有多個結果,一個結果節點不止一個),有沒有辦法避免可能的開銷(可能是由名稱空間管理器引入的),輕松訪問經度/緯度值? 因為不需要解析整個響應,我的c#代碼是否可以重寫以更有效地檢索XML響應?
您可以獲得的最小/最短響應是通過將location屬性和responseattributes設置為“ none”,將其與“ maxresults = 1”結合使用,僅根據我們的輸入獲得第一,最佳結果。 參見以下示例。
這仍然包括導航位置(始終附加到路段)和顯示位置。 后來成為包裹質心又稱屋頂,用於精確的門牌號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.