I have been following some online examples and for some reason I cannot successfully deserialize the following XML document:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<postcode>BD1 1LT</postcode>
<geo>
<lat>53.793063240118215</lat>
<lng>-1.7540318423563699</lng>
<easting>416300.0</easting>
<northing>433000.0</northing>
<geohash>http://geohash.org/gcwf00dz21g1</geohash>
</geo>
<administrative>
<council>
<title>Bradford</title>
<uri>http://statistics.data.gov.uk/id/statistical-geography/E08000032</uri>
<code>E08000032</code>
</council>
<ward>
<title>City</title>
<uri>http://statistics.data.gov.uk/id/statistical-geography/E05001347</uri>
<code>E05001347</code>
</ward>
<constituency>
<title>Bradford West</title>
<uri>http://statistics.data.gov.uk/id/statistical-geography/E14000589</uri>
<code>E14000589</code>
</constituency>
</administrative>
</result>
I've tried all sorts of combinations:
[DataContract(Name = "result", Namespace = "")]
public class Result
{
[DataMember(Name = "postcode")]
public string Postcode { get; set; }
[DataMember(Name = "geo")]
public Geo Geo { get; set; }
}
[DataContract(Name = "geo")]
public class Geo
{
[DataMember(Name = "lat")]
public string Lat { get; set; }
}
But all I can get is the post code and nothing else. Above is what I have working atm.
Below is what I have put together following some examples:
[Test]
public void TestRestCall()
{
var url = "http://uk-postcodes.com/postcode/";
var urlParameters = "bd11lt.xml";
var client = new HttpClient();
client.BaseAddress = new Uri(url);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
// List data response.
HttpResponseMessage response = client.GetAsync(urlParameters).Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
var dataObjects = response.Content.ReadAsAsync<Result>().Result;
//Console.WriteLine("{0}", dataObjects.Geo.);
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
UPDATE:
The Geo property is null and that's as far as I can get with this.