为什么这个GoogleAPI XML响应不能被反序列化
本文关键字:不能 反序列化 响应 XML GoogleAPI 为什么 | 更新日期: 2023-09-27 18:18:57
我试图通过谷歌API获得地址的坐标。这是我的请求:http://maps.googleapis.com/maps/api/geocode/xml?address=" + "Berlin Hbf (tief)"+",传感器= true"这是来自Google的响应:
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>transit_station</type>
<type>point_of_interest</type>
<type>establishment</type>
<formatted_address>Berlin Hbf (tief), 10557 Berlin, Germany</formatted_address>
<address_component>
<long_name>Berlin Hbf (tief)</long_name>
<short_name>Berlin Hbf (tief)</short_name>
<type>point_of_interest</type>
<type>establishment</type>
</address_component>
<address_component>
<long_name>Mitte</long_name>
<short_name>Mitte</short_name>
<type>sublocality_level_1</type>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Berlin</long_name>
<short_name>Berlin</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Berlin</long_name>
<short_name>Berlin</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Germany</long_name>
<short_name>DE</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>10557</long_name>
<short_name>10557</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>52.5253840</lat>
<lng>13.3692087</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>52.5240350</lat>
<lng>13.3678597</lng>
</southwest>
<northeast>
<lat>52.5267330</lat>
<lng>13.3705577</lng>
</northeast>
</viewport>
</geometry>
<place_id>ChIJ0SkfA5ZRqEcRo-ThHeCZJl8</place_id>
</result>
</GeocodeResponse>
这是我用来反序列化它的代码:
string servResp = await response.Content.ReadAsStringAsync();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(GoogleAPI.GeocodeResponse));
geoCodeResponse = ((GoogleAPI.GeocodeResponse)(serializer.Deserialize(XmlReader.Create(servResp))));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
这些是我的类:http://codeshare.io/qGChA
由于某些原因,它崩溃并产生一个异常:
美元例外。InnerException{系统。ArgumentException:非法路径中的字符。在System.IO.Path.CheckInvalidPathChars(字符串在System.IO.Path.GetFileName(字符串在System.IO.FileStream..字符串路径,FileMode模式FileAccess访问,FileShare共享,Int32 bufferSize) atSystem.Xml.XmlRelativePathResolver.OpenStream (Uriuri)}系统。异常{系统。ArgumentException}
为什么会这样?
反序列化方法应该提供StringReader对象,而不是字符串。正确的解决方案是:
string servResp = await response.Content.ReadAsStringAsync();
StringReader stringReader = new StringReader(servResp);
try
{
XmlSerializer serializer = new XmlSerializer(typeof(GoogleAPI.GeocodeResponse));
geoCodeResponse = ((GoogleAPI.GeocodeResponse)(serializer.Deserialize(XmlReader.Create(stringReader))));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
XmlReader.Create(string)
从输入参数指定的URI中读取XML。您传递的是实际的XML,而不是引用它的URI。您需要通过StringWriter
提供XML字符串,如下面的扩展方法所示:
public static class XmlExtensions
{
public static T DeserializeXml<T>(this string xmlString)
{
using (var reader = new StringReader(xmlString))
{
return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
}
}
}
然后做:
string servResp = await response.Content.ReadAsStringAsync();
try
{
geoCodeResponse = servResp.DeserializeXml<GoogleAPI.GeocodeResponse>();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}