public long GetDistance(string a1, string a2) { long result = 0; try { string query = $"https://maps.googleapis.com/maps/api/directions/xml?origin=+{a1}+&destination=+{a2}+&sensor=false&alternatives=false&key=AIzaSyBEnVr56lRcRHGGX31OV8_Sj2AI1zzgekk"; WebRequest request = WebRequest.Create(query); WebResponse response = request.GetResponse(); Stream data = response.GetResponseStream(); StreamReader reader = new StreamReader(data); // json-formatted string from maps api string responseFromServer = reader.ReadToEnd(); Xml.DirectionsResponse distance = null; // string path = cars.xml; XmlSerializer serializer = new XmlSerializer(typeof(Xml.DirectionsResponse)); StreamReader reader1 = new StreamReader(data); // distance = (Xml.DirectionsResponse)serializer.Deserialize(reader); reader1.Close(); response.Close(); using (XmlReader readerXml = XmlReader.Create(new StringReader(responseFromServer))) { distance = (Xml.DirectionsResponse)serializer.Deserialize(readerXml); } long.TryParse(distance.Route.Leg.Distance.Value, out result); result = result / 1000; } catch (Exception e) { Console.WriteLine(e); } return(result); }
double GetXmlFromFileDistance() { try { Xml.DirectionsResponse cars = null; string responseFromServer; XmlSerializer serializer = new XmlSerializer(typeof(Xml.DirectionsResponse)); using (XmlReader readerXml = XmlReader.Create(@"XMLFile1.xml")) { cars = (Xml.DirectionsResponse)serializer.Deserialize(readerXml); } } catch (Exception e) { Console.WriteLine(e); } return(0); }