Example #1
0
        public static Collection <WeatherLocation> SearchCity(string city)
        {
            var    result = new Collection <WeatherLocation>();
            string str;

            using (var client = new WebClient())
            {
                try
                {
                    str = client.DownloadString(SearchUrl + city);
                }
                catch (WebException)
                {
                    return(result);
                }
            }
            try
            {
                //var data = new XmlDocument();
                //data.LoadXml(str);
                string json         = str;
                var    deserializer = new JavaScriptSerializer();
                var    server       = deserializer.Deserialize <WeatherComSearch.Rootobject>(json);


                //var elements = data.SelectNodes("//RESULTS");

                if (server != null)
                {
                    foreach (var element in server.RESULTS)
                    {
                        // string tempID = element.l.Replace("/q/locid:", "");
                        // tempID = tempID.Substring(0, tempID.IndexOf(";")+1);


                        var temp = new WeatherLocation
                        {
                            LocId   = element.l,
                            LocType = element.type,
                            Name    = element.name
                        };
                        result.Add(temp);
                    }
                }
            }
            catch (XmlException) { }
            catch (XPathException) { }


            return(result);
        }
Example #2
0
        public static Collection <WeatherLocation> SearchCity(string city)
        {
            var    result = new Collection <WeatherLocation>();
            string str;

            using (var client = new WebClient())
            {
                try
                {
                    str = client.DownloadString(SearchUrl + city);
                }
                catch (WebException)
                {
                    return(result);
                }
            }
            try
            {
                var data = new XmlDocument();
                data.LoadXml(str);
                var elements = data.SelectNodes("//search/loc");
                if (elements != null)
                {
                    foreach (XmlElement element in elements)
                    {
                        var temp = new WeatherLocation
                        {
                            LocId   = element.GetAttribute("id"),
                            LocType = element.GetAttribute("type"),
                            Name    = element.InnerText
                        };
                        result.Add(temp);
                    }
                }
            }
            catch (XmlException) { }
            catch (XPathException) { }
            return(result);
        }
Example #3
0
        public static Collection<WeatherLocation> SearchCity(string city)
        {
            var result = new Collection<WeatherLocation>();
            string str;

            using (var client = new WebClient())
            {
                try
                {
                    str = client.DownloadString(SearchUrl + city);
                }
                catch (WebException)
                {
                    return result;
                }

            }
            try
            {
                //var data = new XmlDocument();
                //data.LoadXml(str);
                string json = str;
                var deserializer = new JavaScriptSerializer();
                var server = deserializer.Deserialize<WeatherComSearch.Rootobject>(json);


                //var elements = data.SelectNodes("//RESULTS");
                
                if (server != null)
                    foreach (var element in server.RESULTS)
                    {

                       // string tempID = element.l.Replace("/q/locid:", "");
                       // tempID = tempID.Substring(0, tempID.IndexOf(";")+1);
                        
                        
                        var temp = new WeatherLocation
                                       {
                                           LocId = element.l,
                                           LocType = element.type,
                                           Name = element.name
                                       };
                        result.Add(temp);
                    }
            }
            catch (XmlException) { }
            catch (XPathException) { }
            
            
            return result;
        }