【发布时间】:2015-10-22 09:34:30
【问题描述】:
我正在尝试通过 Google API 获取地址的坐标。这是我的要求:http://maps.googleapis.com/maps/api/geocode/xml?address=" + "Berlin Hbf (tief)" + "&sensor=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
由于某种原因,它会因异常而崩溃:
$exception.InnerException {System.ArgumentException: 非法 路径中的字符。在 System.IO.Path.CheckInvalidPathChars(字符串 路径,布尔检查附加)在 System.IO.Path.GetFileName(字符串 路径)在 System.IO.FileStream..ctor(字符串路径,文件模式模式, FileAccess 访问、FileShare 共享、Int32 bufferSize) 在 System.Xml.XmlRelativePathResolver.OpenStream(Uri uri)} System.Exception {System.ArgumentException}
这是为什么?
【问题讨论】:
-
我会说这不是 XML 问题。访问“文件”/内容时使用的路径是什么?
-
我不知道没有任何文件涉及..
-
您确定要将流传递给 xmlreader 吗?如果您的 strserverresponse 是一个字符串,它应该是一个 url。见msdn.microsoft.com/de-de/library/…
-
是的,请参阅我更新的问题。
-
你确定你正在传递一个流?但是您的代码状态为
string servResp。 ?!?!?
标签: c# xml api windows-phone-8.1 xml-deserialization