Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserialize google geocoding XML using Datacontract in C#
    text
    copied!<p>I have the following Google geocoding XML</p> <pre><code>&lt;GeocodeResponse&gt; &lt;status&gt;OK&lt;/status&gt; &lt;result&gt; &lt;type&gt;street_address&lt;/type&gt; &lt;formatted_address&gt;1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA&lt;/formatted_address&gt; &lt;address_component&gt; &lt;long_name&gt;1600&lt;/long_name&gt; &lt;short_name&gt;1600&lt;/short_name&gt; &lt;type&gt;street_number&lt;/type&gt; &lt;/address_component&gt; &lt;address_component&gt; &lt;long_name&gt;Amphitheatre Pkwy&lt;/long_name&gt; &lt;short_name&gt;Amphitheatre Pkwy&lt;/short_name&gt; &lt;type&gt;route&lt;/type&gt; &lt;/address_component&gt; .... &lt;geometry&gt; &lt;location&gt; &lt;lat&gt;37.4217550&lt;/lat&gt; &lt;lng&gt;-122.0846330&lt;/lng&gt; &lt;/location&gt; &lt;location_type&gt;ROOFTOP&lt;/location_type&gt; &lt;viewport&gt; &lt;southwest&gt; &lt;lat&gt;37.4188514&lt;/lat&gt; &lt;lng&gt;-122.0874526&lt;/lng&gt; &lt;/southwest&gt; &lt;northeast&gt; &lt;lat&gt;37.4251466&lt;/lat&gt; &lt;lng&gt;-122.0811574&lt;/lng&gt; &lt;/northeast&gt; &lt;/viewport&gt; &lt;/geometry&gt; &lt;/result&gt; &lt;/GeocodeResponse&gt; </code></pre> <p>And the following object</p> <pre><code>[DataContract(Namespace = "")] public class GeocodeResponse { [DataMember(Name = "status", Order = 1)] public string Status { get; set; } [DataMember(Name = "result", Order = 2)] public List&lt;Result&gt; Results { get; set; } [DataContract(Name = "result", Namespace = "")] public class Result { [DataMember(Name = "geometry")] public CGeometry Geometry { get; set; } [DataContract(Name = "geometry", Namespace = "")] public class CGeometry { [DataMember(Name = "location")] public CLocation Location { get; set; } [DataContract(Name = "location", Namespace = "")] public class CLocation { [DataMember(Name = "lat", Order = 1)] public double Lat { get; set; } [DataMember(Name = "lng", Order = 2)] public double Lng { get; set; } } } } } </code></pre> <p>I am trying to deserialize using the following method</p> <pre><code>DataContractSerializer serializer = new DataContractSerializer(typeof(GeocodeResponse)); var response = (GeocodeResponse)serializer.ReadObject(request.GetResponse().GetResponseStream()); </code></pre> <p>After deserialization, Results is always empty. What am I doing wrong? </p> <p>UPDATE:</p> <p>Changed Result element. Getting another error now:</p> <p><strong>There was an error deserializing the object of type GeocodeResponse. The data at the root level is invalid. Line 1, position 1.</strong></p> <pre><code>... [DataMember(Name = "result", Order = 2)] public CResult Result { get; set; } [DataContract] public class CResult { ... </code></pre> <p>I am able to Deserialize the original object using JSON like below.</p> <pre><code>DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeocodeResponse)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload