为什么无法反序列化此GoogleAPI XML响应?

时间:2015-10-22 09:34:30

标签: c# xml api windows-phone-8.1 xml-deserialization

我正在尝试通过Google API获取地址的坐标。这是我的要求:http://maps.googleapis.com/maps/api/geocode/xml?address=" +"柏林Hbf(tief)" +"& sensor = true" 这是谷歌的回应:

<?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(String   在System.IO.Path.GetFileName(String。的路径,布尔检查附加)   在System.IO.FileStream..ctor(String路径,FileMode模式,   FileAccess访问,FileShare共享,Int32 bufferSize)at   System.Xml.XmlRelativePathResolver.OpenStream(URI   uri)} System.Exception {System.ArgumentException}

为什么会这样?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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