You can enter either of these URLs into a browser and verify that they return valid xml:
http://maps.googleapis.com/maps/api/geocode/xml?`address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"
...or json:
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false%22
...but when attempting to get the xml programmatically using this code (after installing Json.NET via NuGet into my project and dropping a dataGridView on my Windows form):
dataGridView1.DataSource = GetLocationData("http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
private JArray GetLocationData(string uri)
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JArray>(s);
}
...I get: Newtonsoft.Json.JsonReaderException was unhandled _message=Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
I thought, okay, this actually is json code, not xml, so I replaced "xml" with "json" in the URL, expecting more joy in Mudville.
However, when attempting to get the same data as json, I also get an exception, namely, "System.InvalidCastException was unhandled _message=Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'."
Why, and how can I fix it? Is it possible to also grab the data as xml?