我构建了一个小程序,它从 Google Maps API 地理编码服务读取 XML 输出并使用 LINQ to XML 解析字符串。
如果返回的 XML 包含非 ASCII 字符,那么我的输出似乎中断了。有没有办法以不同的方式读取/编码?
这是代码关键部分的快照。
public static void Read(IList<string> LocationDetails, string Type)
{
using (WebClient webClient = new WebClient())
{
webClient.Proxy = null;
for(int i = 0; i < 5; i++)
{
//Generate geocode request and read XML file to string
string request = String.Format("Https://maps.google.com/maps/api/geocode/xml?{0}={1}&sensor=false", Type, LocationDetails[i]);
string locationXML = webClient.DownloadString(request);
XElement root = XElement.Parse(locationXML);
//Check if request is OK or otherwise
if (root.Element("status").Value != "OK")
{ //Skip to next iteration if status not OK
continue;
}
}
.....跳过一些声明代码。StateName 声明为字符串。
try
{
StateName = (result.Elements("address_component")
.Where(x => (string)x.Element("type") == "administrative_area_level_1")
.Select(x => x.Element("long_name").Value).First());
}
catch (InvalidOperationException e)
{
StateName = null;
}