Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't really use the <code>DataContractSerializer</code> (DCS) to deserialize the response from that request, you need to use the <code>XmlSerializer</code>. DCS doesn't support unwrapped collections, which is what the response contains - like the one shown below.</p> <pre><code>&lt;a&gt; &lt;b/&gt; &lt;c/&gt; &lt;c/&gt; &lt;c/&gt; &lt;/a&gt; </code></pre> <p>The DCS only supports collections when they're wrapped in their own element:</p> <pre><code>&lt;a&gt; &lt;b/&gt; &lt;cs&gt; &lt;c/&gt; &lt;c/&gt; &lt;c/&gt; &lt;/cs&gt; &lt;/a&gt; </code></pre> <p>The <code>XmlSerializer</code> does support such collections. The code below shows a partial class structure for deserializing a response from the Google geocoding XML.</p> <pre><code>public class StackOverflow_7521821 { [XmlRoot(ElementName = "GeocodeResponse", Namespace = "")] public class GeocodeResponse { [XmlElement(ElementName = "status")] public GeocodeResponseStatusCode Status; [XmlElement(ElementName = "result")] public List&lt;GeocodeResponseResult&gt; Results; } [XmlType(Namespace = "")] public class GeocodeResponseResult { [XmlElement(ElementName = "type")] public List&lt;string&gt; Types; [XmlElement(ElementName = "formatted_address")] public string FormattedAddress; [XmlElement(ElementName = "address_component")] public List&lt;GeocodeResponseAddressComponent&gt; AddressComponents; [XmlElement(ElementName = "geometry")] public GeocodeResponseResultGeometry Geometry; } [XmlType(Namespace = "")] public class GeocodeResponseAddressComponent { [XmlElement(ElementName = "long_name")] public string LongName; [XmlElement(ElementName = "short_name")] public string ShortName; [XmlElement(ElementName = "type")] public List&lt;string&gt; Types; } [XmlType(Namespace = "")] public class GeocodeResponseResultGeometry { [XmlElement(ElementName = "location")] public Location Location; [XmlElement(ElementName = "location_type")] public GeocodeResponseResultGeometryLocationType LocationType; [XmlElement(ElementName = "viewport")] public GeocodeResponseResultGeometryViewport Viewport; } [XmlType(Namespace = "")] public class GeocodeResponseResultGeometryViewport { [XmlElement(ElementName = "southwest")] public Location Southwest; [XmlElement(ElementName = "northeast")] public Location Northeast; } public enum GeocodeResponseStatusCode { OK, ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED, INVALID_REQUEST, } public enum GeocodeResponseResultGeometryLocationType { ROOFTOP, RANGE_INTERPOLATED, GEOMETRIC_CENTER, APPROXIMATE, } [XmlType(Namespace = "")] public class Location { [XmlElement(ElementName = "lat")] public string Lat; [XmlElement(ElementName = "lng")] public string Lng; } public static void Test() { XmlSerializer xs = new XmlSerializer(typeof(GeocodeResponse)); WebClient c = new WebClient(); byte[] response = c.DownloadData("http://maps.googleapis.com/maps/api/geocode/xml?address=1+Microsoft+Way,+Redmond,+WA&amp;sensor=true"); MemoryStream ms = new MemoryStream(response); GeocodeResponse geocodeResponse = (GeocodeResponse)xs.Deserialize(ms); Console.WriteLine(geocodeResponse); c = new WebClient(); response = c.DownloadData("http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&amp;sensor=true"); ms = new MemoryStream(response); geocodeResponse = (GeocodeResponse)xs.Deserialize(ms); Console.WriteLine(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