Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            city = cityChange.Text;
            string    url     = "http://api.openweathermap.org/data/2.5/weather?q=" + city + mode + "&units=" + units + "&appid=01262a407ed6af1b08040793da605511";
            XDocument weather = new XDocument();

            if (city != "")
            {
                weather = XDocument.Load(url);

                foreach (XElement TemElement in weather.Element("current").Elements("city"))
                {
                    XAttribute idAttribute   = TemElement.Attribute("id");
                    XAttribute cityAttribute = TemElement.Attribute("name");
                    idStr       = (string)idAttribute;
                    cityStr     = (string)cityAttribute;
                    label1.Text = "id: " + idStr + "\ncity: " + cityStr;
                }

                foreach (XElement TemElement in weather.Element("current").Elements("temperature"))
                {
                    XAttribute valueAttribute = TemElement.Attribute("value");
                    valueStr = valueAttribute.Value;
                    if (comboBox1.SelectedItem.ToString() == "Фаренгейты")
                    {
                        //double valueDbl = Convert.ToDouble(valueStr);
                        double valueDbl = Double.Parse(valueStr);
                        valueDbl = valueDbl * 1.8 + 32;
                        valueStr = Convert.ToString(valueDbl);
                    }
                    label1.Text += "\ntemperature: " + valueStr;
                }
            }
        }
Exemplo n.º 2
0
        private static void GetWeather(string method, string city, ref WeatherTxt weatherTxt)
        {
            //WeatherTxt weatherTxt = new WeatherTxt();
            string appId = File.ReadAllText(@"appId.txt");
            string url   = "http://api.openweathermap.org/data/2.5/" + method + "?q=" + city + "&mode=xml&units=metric" + appId;
            string data  = @"C:\Users\Владимир\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\WeatherXml\" + Regex.Replace(System.DateTime.Now.ToLongTimeString(), "[:]", ".") + ".xml";

            Console.WriteLine(data);

            XDocument weather = new XDocument();

            weather = XDocument.Load(url);
            weather.Save(data);

            if (method == "weather")
            {
                foreach (XElement TemElement in weather.Element("current").Elements("temperature"))
                {
                    XAttribute valueAttribute = TemElement.Attribute("value");
                    weatherTxt.SetBody(valueAttribute.Value);
                }
                weatherTxt.BodyToTxt(weatherTxt.getBody());
            }
            else if (method == "forecast")
            {
                foreach (XElement TimeElement in weather.Element("weatherdata").Element("forecast").Elements("time"))
                {
                    XAttribute valueAttribute = TimeElement.Element("temperature").Attribute("value");
                    weatherTxt.SetBody(valueAttribute.Value);
                }
                weatherTxt.BodyToTxt(weatherTxt.getBody());
            }

            else if (method == "forecast/daily")
            {
                foreach (XElement TimeElement in weather.Element("weatherdata").Element("forecast").Elements("time"))
                {
                    XAttribute valueAttribute = TimeElement.Element("temperature").Attribute("day");
                    weatherTxt.SetBody(valueAttribute.Value);
                }
                weatherTxt.BodyToTxt(weatherTxt.getBody());
            }
        }
Exemplo n.º 3
0
        public WeatherTxt GetWeather(string method, string city)
        {
            url  = "http://api.openweathermap.org/data/2.5/" + method + "?q=" + city + "&mode=xml&units=metric" + appId;
            date = @"H:\Users\denis\\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\\Debug\WeatherXml\" + Regex.Replace(System.DateTime.Now.ToLongTimeString(), "[:]", ".") + ".xml";
            Console.WriteLine(date);

            XDocument weather = new XDocument();

            weather = XDocument.Load(url);
            weather.Save(date);

            if (method == "weather")
            {
                foreach (XElement TemElement in weather.Element("current").Elements("temperature"))
                {
                    XAttribute valueAttribute = TemElement.Attribute("value");
                    SetBody(valueAttribute.Value);
                }
                BodyToTxt(getBody());
            }
            else if (method == "forecast")
            {
                foreach (XElement TimeElement in weather.Element("weatherdata").Element("forecast").Elements("time"))
                {
                    XAttribute valueAttribute = TimeElement.Element("temperature").Attribute("value");
                    SetBody(valueAttribute.Value);
                }
                BodyToTxt(getBody());
            }
            else if (method == "forecast/daily")
            {
                foreach (XElement TimeElement in weather.Element("weatherdata").Element("forecast").Elements("time"))
                {
                    XAttribute valueAttribute = TimeElement.Element("temperature").Attribute("day");
                    SetBody(valueAttribute.Value);
                }
                BodyToTxt(getBody());
            }

            WeatherTxt weatherTxt = new WeatherTxt(getBodyTxt(), getBody());

            return(weatherTxt);
        }