Exemple #1
0
        /// <summary>
        /// get weather with city
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public Weather GetWeather(string city)
        {
            const string baseUrl    = @"https://www.google.com";
            UrlBuilder   urlBuilder = new UrlBuilder(string.Format(@"{0}/ig/api", baseUrl));

            urlBuilder.Add("hl", "zh-cn");
            urlBuilder.Add("weather", city);

            string      weatherXml  = webHelper.Get(urlBuilder);
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(weatherXml);
            XmlNodeList nodeCity = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_information");

            Weather.CityInfomaition cityInfo = new Weather.CityInfomaition(
                nodeCity.Item(0).SelectSingleNode("city").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("postal_code").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("latitude_e6").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("longitude_e6").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("unit_system").Attributes["data"].InnerText,
                Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("forecast_date").Attributes["data"].InnerText),
                Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("current_date_time").Attributes["data"].InnerText));
            XmlNodeList nodeToday = xmlDocument.SelectNodes("xml_api_reply/weather/current_conditions");

            Weather.TodayWeather today = new Weather.TodayWeather(
                Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText),
                Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText),
                nodeToday.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText,
                nodeToday.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText,
                nodeToday.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText,
                new ImageHelper(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText).Image);

            XmlNodeList nodeList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions");

            Weather.DayWeather[] dayWeathers = new Weather.DayWeather[nodeList.Count];
            for (int i = 0; i < nodeList.Count; i++)
            {
                string             dayOfWeek  = nodeList.Item(i).SelectSingleNode("day_of_week").Attributes["data"].InnerText;
                string             height     = nodeList.Item(i).SelectSingleNode("high").Attributes["data"].InnerText;
                string             width      = nodeList.Item(i).SelectSingleNode("low").Attributes["data"].InnerText;
                string             condition  = nodeList.Item(i).SelectSingleNode("condition").Attributes["data"].InnerText;
                string             icon       = nodeList.Item(i).SelectSingleNode("icon").Attributes["data"].InnerText;
                Weather.DayWeather dayWeather = new Weather.DayWeather(
                    dayOfWeek,
                    Convert.ToInt16(height),
                    Convert.ToInt16(width),
                    condition,
                    new ImageHelper(string.Concat(baseUrl, icon)).Image
                    );
                dayWeathers[i] = dayWeather;
            }
            Weather weather = new Weather(cityInfo, today, dayWeathers);

            return(weather);
        }
Exemple #2
0
        public static Weather GoogleWeather(string city)
        {
            const string baseUrl        = @"https://www.google.com";
            WebHelper    connectionBase = new WebHelper();
            UrlBuilder   parameters     = new UrlBuilder(string.Format(@"{0}/ig/api", baseUrl));

            parameters.Add("hl", "zh-cn");
            parameters.Add("weather", city);
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(connectionBase.Get(parameters.ToString()));
            XmlNodeList nodeCity = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_information");

            Weather.CityInfomaition cityInfo = new Weather.CityInfomaition(
                nodeCity.Item(0).SelectSingleNode("city").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("postal_code").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("latitude_e6").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("longitude_e6").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("unit_system").Attributes["data"].InnerText,
                Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("forecast_date").Attributes["data"].InnerText),
                Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("current_date_time").Attributes["data"].InnerText));
            XmlNodeList nodeToday = xmlDocument.SelectNodes("xml_api_reply/weather/current_conditions");

            Weather.TodayWeather today = new Weather.TodayWeather(
                Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText),
                Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText),
                nodeToday.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText,
                nodeToday.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText,
                nodeToday.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText,
                ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
            XmlNodeList nodeList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions");

            Weather.DayWeather tomorrow = new Weather.DayWeather(
                Convert.ToInt16(nodeList.Item(1).SelectSingleNode("high").Attributes["data"].InnerText),
                Convert.ToInt16(nodeList.Item(1).SelectSingleNode("low").Attributes["data"].InnerText),
                nodeList.Item(1).SelectSingleNode("condition").Attributes["data"].InnerText,
                ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
            Weather.DayWeather third = new Weather.DayWeather(
                Convert.ToInt16(nodeList.Item(2).SelectSingleNode("high").Attributes["data"].InnerText),
                Convert.ToInt16(nodeList.Item(2).SelectSingleNode("low").Attributes["data"].InnerText),
                nodeList.Item(2).SelectSingleNode("condition").Attributes["data"].InnerText,
                ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
            Weather.DayWeather fourth = new Weather.DayWeather(
                Convert.ToInt16(nodeList.Item(3).SelectSingleNode("high").Attributes["data"].InnerText),
                Convert.ToInt16(nodeList.Item(3).SelectSingleNode("low").Attributes["data"].InnerText),
                nodeList.Item(3).SelectSingleNode("condition").Attributes["data"].InnerText,
                ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
            Weather weather = new Weather(cityInfo, today, tomorrow, third, fourth);

            return(weather);
        }
        /// <summary>
        /// get weather with city
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public Weather GetWeather(string city)
        {
            const string baseUrl = @"https://www.google.com";
            UrlBuilder urlBuilder = new UrlBuilder(string.Format(@"{0}/ig/api", baseUrl));
            urlBuilder.Add("hl", "zh-cn");
            urlBuilder.Add("weather", city);

            string weatherXml = webHelper.Get(urlBuilder);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(weatherXml);
            XmlNodeList nodeCity = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_information");
            Weather.CityInfomaition cityInfo = new Weather.CityInfomaition(
                nodeCity.Item(0).SelectSingleNode("city").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("postal_code").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("latitude_e6").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("longitude_e6").Attributes["data"].InnerText,
                nodeCity.Item(0).SelectSingleNode("unit_system").Attributes["data"].InnerText,
                Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("forecast_date").Attributes["data"].InnerText),
                Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("current_date_time").Attributes["data"].InnerText));
            XmlNodeList nodeToday = xmlDocument.SelectNodes("xml_api_reply/weather/current_conditions");
            Weather.TodayWeather today = new Weather.TodayWeather(
                Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText),
                Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText),
                nodeToday.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText,
                nodeToday.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText,
                nodeToday.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText,
                new ImageHelper(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText).Image);

            XmlNodeList nodeList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions");
            Weather.DayWeather[] dayWeathers = new Weather.DayWeather[nodeList.Count];
            for (int i = 0; i < nodeList.Count; i++)
            {
                string dayOfWeek = nodeList.Item(i).SelectSingleNode("day_of_week").Attributes["data"].InnerText;
                string height = nodeList.Item(i).SelectSingleNode("high").Attributes["data"].InnerText;
                string width = nodeList.Item(i).SelectSingleNode("low").Attributes["data"].InnerText;
                string condition = nodeList.Item(i).SelectSingleNode("condition").Attributes["data"].InnerText;
                string icon = nodeList.Item(i).SelectSingleNode("icon").Attributes["data"].InnerText;
                Weather.DayWeather dayWeather = new Weather.DayWeather(
                    dayOfWeek,
                    Convert.ToInt16(height),
                    Convert.ToInt16(width),
                    condition,
                    new ImageHelper(string.Concat(baseUrl, icon)).Image
                    );
                dayWeathers[i] = dayWeather;
            }
            Weather weather = new Weather(cityInfo, today, dayWeathers);
            return weather;
        }
 public static Weather GoogleWeather(string city)
 {
     const string baseUrl = @"https://www.google.com";
     WebHelper connectionBase = new WebHelper();
     UrlBuilder parameters = new UrlBuilder(string.Format(@"{0}/ig/api", baseUrl));
     parameters.Add("hl","zh-cn");
     parameters.Add("weather",city);
     XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(connectionBase.Get(parameters.ToString()));
     XmlNodeList nodeCity = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_information");
     Weather.CityInfomaition cityInfo = new Weather.CityInfomaition(
         nodeCity.Item(0).SelectSingleNode("city").Attributes["data"].InnerText,
         nodeCity.Item(0).SelectSingleNode("postal_code").Attributes["data"].InnerText,
         nodeCity.Item(0).SelectSingleNode("latitude_e6").Attributes["data"].InnerText,
         nodeCity.Item(0).SelectSingleNode("longitude_e6").Attributes["data"].InnerText,
         nodeCity.Item(0).SelectSingleNode("unit_system").Attributes["data"].InnerText,
         Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("forecast_date").Attributes["data"].InnerText),
         Convert.ToDateTime(nodeCity.Item(0).SelectSingleNode("current_date_time").Attributes["data"].InnerText));
     XmlNodeList nodeToday = xmlDocument.SelectNodes("xml_api_reply/weather/current_conditions");
     Weather.TodayWeather today = new Weather.TodayWeather(
         Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText),
         Convert.ToInt16(nodeToday.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText),
         nodeToday.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText,
         nodeToday.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText,
         nodeToday.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText,
         ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
     XmlNodeList nodeList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions");
     Weather.DayWeather tomorrow = new Weather.DayWeather(
         Convert.ToInt16(nodeList.Item(1).SelectSingleNode("high").Attributes["data"].InnerText),
         Convert.ToInt16(nodeList.Item(1).SelectSingleNode("low").Attributes["data"].InnerText),
         nodeList.Item(1).SelectSingleNode("condition").Attributes["data"].InnerText,
         ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
     Weather.DayWeather third = new Weather.DayWeather(
         Convert.ToInt16(nodeList.Item(2).SelectSingleNode("high").Attributes["data"].InnerText),
         Convert.ToInt16(nodeList.Item(2).SelectSingleNode("low").Attributes["data"].InnerText),
         nodeList.Item(2).SelectSingleNode("condition").Attributes["data"].InnerText,
         ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
     Weather.DayWeather fourth = new Weather.DayWeather(
         Convert.ToInt16(nodeList.Item(3).SelectSingleNode("high").Attributes["data"].InnerText),
         Convert.ToInt16(nodeList.Item(3).SelectSingleNode("low").Attributes["data"].InnerText),
         nodeList.Item(3).SelectSingleNode("condition").Attributes["data"].InnerText,
         ImageHelper.GetImage(baseUrl + nodeToday.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText));
     Weather weather = new Weather(cityInfo,today, tomorrow, third, fourth);
     return weather;
 }