【问题标题】:Why this GoogleAPI XML response cannot be deserialized?为什么无法反序列化此 GoogleAPI XML 响应?
【发布时间】: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


【解决方案1】:

Deserialize 方法应该提供一个 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);
            }

【讨论】:

    【解决方案2】:

    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);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多