internal void Request(string street, string city, string stateCode, string zipCode, EventHandler requestCompletedHandler) { m_geo = this; Street = street; City = city; StateCode = stateCode; ZipCode = zipCode; m_RequestCompletedHandler = requestCompletedHandler; string url = ""; if (m_geo.Street.Length > 0) url += "&street=" + m_geo.Street; if (m_geo.City.Length > 0) url += "&city=" + m_geo.City; if (m_geo.StateCode.Length == 2) url += "&state=" + m_geo.StateCode; if (m_geo.ZipCode.Length >= 5) url += (m_bYahoo ? "&zip=" : "&postalcode=") + m_geo.ZipCode; url = m_apiURL + url; url = url.Replace(" ", "+"); // Yahoo example shows + sign instead of spaces. WebClient webClient = new WebClient(); try { #if SIMPLE webClient.DownloadStringCompleted += RequestCompleted; webClient.DownloadStringAsync(new Uri(url)); #else webClient.OpenReadCompleted += RequestComplete; webClient.OpenReadAsync(new Uri(url)); #endif } catch (SecurityException ex) { // handle synchronous exception ex.DebugOutput(); } }
private void OnGeoAddressClick(object sender, RoutedEventArgs e) { GeoAddress geo = new GeoAddress(); geo.Request(""/*street*/, ""/*city*/, ""/*stateCode*/, "01810"/*zipCode*/, GeoAddress_RequestCompleted); }
public void RequestComplete(object sender, OpenReadCompletedEventArgs args) #endif { object result = null; try { // Wrap the access "results" in order to catch exceptions during an asynchronous web request result = args.Result; } catch (SecurityException) { } bool bNoResults = (result == null ? true : string.IsNullOrEmpty(result.ToString())); if (args.Error != null || args.Cancelled || bNoResults) { if (m_RequestCompletedHandler != null) { if (args.Cancelled) m_geo.ErrorMessage = "Cancelled"; else if (bNoResults) m_geo.ErrorMessage = "No results returned"; else m_geo.ErrorMessage = args.Error.Message; m_RequestCompletedHandler.Invoke(m_geo, args); } return; } //m_geo.Street //m_geo.City //m_geo.StateCode //m_geo.ZipCode m_geo.Latitude = ""; m_geo.Longitude = ""; m_geo.Precision = ""; m_geo.Warning = ""; m_geo.ErrorMessage = ""; try { #if SIMPLE XDocument xmlDoc = XDocument.Parse(args.Result); // xmlDoc.Root.Name should be "geonames" or null #else XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreProcessingInstructions = true; settings.IgnoreComments = true; Stream stream = args.Result; XmlReader reader = XmlReader.Create(stream, settings); XDocument xmlDoc = XDocument.Load(reader); #endif if (xmlDoc.Root != null) // An non-empty result { var query = from item in xmlDoc.Descendants("code") where item.Element("countryCode").Value == "US" orderby item.Element("countryCode").Value ascending select new GeoAddress { City = item.Element("name").Value, StateCode = item.Element("adminCode1").Value, ZipCode = item.Element("postalcode").Value, Latitude = item.Element("lat").Value, Longitude = item.Element("lng").Value, }; int iCount = query.Count(); m_geo = query.First(); //List<GeoAddress> geolist = query.ToList<GeoAddress>(); //List<GeoAddress> geolist = query.Take(3).ToList<GeoAddress>(); } m_geo.Warning = Regex.Replace(Warning, @"<(.|\n)*?>", string.Empty); } catch (WebException webException) { m_geo.ErrorMessage = webException.Message; } if (m_RequestCompletedHandler != null) m_RequestCompletedHandler.Invoke(m_geo, args); }