public bool GetLocationData(City city)
        {
            string locationKey = city.Id;

            if (string.IsNullOrEmpty(locationKey))
            {
                return(false);
            }

            city.HasData = false;
            string cachefile = string.Format(_parsefileLocation, locationKey.Replace(',', '_'));

            XPathDocument doc = null;

            if (ShouldUseCache(cachefile))
            {
                doc = new XPathDocument(cachefile);
            }
            else
            {
                if (!NetworkConnectionTracker.IsNetworkConnected)
                {
                    return(false);
                }

                Dictionary <string, string> args = new Dictionary <string, string>();
                args["q"]           = city.Id;
                args["num_of_days"] = "5";

                string url = BuildRequest("weather.ashx", args);
                doc = WorldWeatherOnlineHelper.GetOnlineContent(url);
                if (doc == null && File.Exists(cachefile))
                {
                    doc = new XPathDocument(cachefile);
                }

                if (doc == null)
                {
                    return(false);
                }

                // Save cache file
                using (XmlWriter xw = XmlWriter.Create(cachefile))
                {
                    doc.CreateNavigator().WriteSubtree(xw);
                    xw.Close();
                }
            }
            return(Parse(city, doc));
        }
Пример #2
0
        /// <summary>
        /// Returns a new List of City objects if the searched
        /// location was found... the unique id (City.Id) and the
        /// location name will be set for each City...
        /// </summary>
        /// <param name="locationName">Name of the location to search for</param>
        /// <returns>New City List</returns>
        public List <CitySetupInfo> FindLocationsByName(string locationName)
        {
            // create list that will hold all found cities
            List <CitySetupInfo> locations = new List <CitySetupInfo>();

            Dictionary <string, string> args = new Dictionary <string, string>();

            args["query"]          = locationName;
            args["num_of_results"] = "5";

            string            url       = BuildRequest("search.ashx", args);
            XPathDocument     doc       = WorldWeatherOnlineHelper.GetOnlineContent(url);
            XPathNavigator    navigator = doc.CreateNavigator();
            XPathNodeIterator nodes     = navigator.Select("/search_api/result");

            while (nodes.MoveNext())
            {
                List <string>  details        = new List <string>();
                CitySetupInfo  city           = new CitySetupInfo();
                XPathNavigator nodesNavigator = nodes.Current;
                if (nodesNavigator == null)
                {
                    continue;
                }

                XPathNavigator areaNode = nodesNavigator.SelectSingleNode("areaName");
                if (areaNode != null)
                {
                    city.Name = areaNode.Value;
                }

                XPathNavigator countryNode = nodesNavigator.SelectSingleNode("country");
                if (countryNode != null)
                {
                    city.Name += " (" + countryNode.Value + ")";
                }

                // Build a unique key based on latitude & longitude
                XPathNavigator latNode = nodesNavigator.SelectSingleNode("latitude");
                XPathNavigator lonNode = nodesNavigator.SelectSingleNode("longitude");
                if (latNode != null && lonNode != null)
                {
                    city.Id = latNode.Value + "," + lonNode.Value;
                    details.Add(string.Format("{0:00.00}  {1:00.00}", latNode.ValueAsDouble, lonNode.ValueAsDouble));
                }

                // Get population info
                XPathNavigator populationNode = nodesNavigator.SelectSingleNode("population");
                if (populationNode != null && populationNode.ValueAsDouble > 0)
                {
                    details.Add(string.Format("Pop.: {0}", populationNode.Value));
                }

                if (details.Count > 0)
                {
                    city.Detail = string.Format("({0})", string.Join(" / ", details.ToArray()));
                }

                city.Grabber = GetServiceName();
                locations.Add(city);
            }
            return(locations);
        }