2016-06-08 123 views
0

尋找獲取當前天氣和天氣預報倫敦 WOEID 44418 https://query.yahooapis.com/v1/public/yql? q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys雅虎天氣預測C#

如何得到預測呢? 以下是今天天氣的代碼。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Xml; 
using MySql.Data.MySqlClient; 
using System.Windows.Forms.DataVisualization.Charting; 
using System.Runtime.InteropServices; 


namespace weatherstation1 
{ 
    public partial class Form2 : Form 
    { 

     string Town; 
     string Temperature; 
     string Temperature2; 
     string Condition; 
     string Humidity; 
     string Windspeed; 
     string ConditionCode; 
     int cityNumber = 44418; 

     public Form2() 
     { 
      InitializeComponent(); 
      Getweather(); 
      GetCondition(); 
      textBox1.Text = Town; 
      textBox3.Text = Condition; 
      textBox4.Text = Humidity; 
      textBox5.Text = Windspeed; 


     } 

     private void toolStripLabel1_Click(object sender, EventArgs e) 
     { 

     } 

     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      AboutBox1 frmAbout = new AboutBox1(); 
      frmAbout.Show(); 
     } 

     private void Form2_Load(object sender, EventArgs e) 
     { 

     } 

     private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 
     { 
      this.Show(); 
     } 

     private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) 
     { 

     } 

     private void aboutToolStripMenuItem1_Click(object sender, EventArgs e) 
     { 
      AboutBox1 frmAbout = new AboutBox1(); 
      frmAbout.Show(); 
     } 


     private void Getweather() 
     { 

      string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"); 

      XmlDocument wData = new XmlDocument(); 
      wData.Load(query); 

      XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable); 
      manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0"); 
      XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel"); 
      XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager); 

      Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value; 
      Temperature2 = ((Double.Parse(Temperature) - 32)/1.8).ToString(); 
      Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value; 
      ConditionCode = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["code"].Value; 
      Time = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["date"].Value; 
      // Windspeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value; 
      // Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value; 




     } 

     private void database() 
     { 
      // string Time = DateTime.Now.ToString("d/M/yyyy HH:mm"); 
      string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456"; 
      MySqlConnection MyConn = new MySqlConnection(MyConnection); 
      string query1 = "INSERT INTO weatherstation1.London"+ 
              "(City, Time, Temperature, Temperature2, WeatherCondition) VALUES('" + Town + 
              "','" + Time + "','" + Temperature + "','" + Temperature2 + "' + Condition + "');"; 
      MySqlCommand MyCommand = new MySqlCommand(query1, MyConn); 
      MySqlDataReader MyReader; 
      MyConn.Open(); 
      MyReader = MyCommand.ExecuteReader(); 





     } 

     public void GetCondition() 
     { 
      string cc = ConditionCode; 
      if (cc <= 'Showers') 
      { 
       weatherCondition.ImageLocation = "showers.png"; 
      } 
      else if (cc <= 'Scattered Showers') 
      { 
       weatherCondition.ImageLocation = "scatteredshowers.png"; 
      } 
      else if (cc <= 'Mostly Cloudy') 
      { 
       weatherCondition.ImageLocation = "mostlycloudy.png"; 
      } 
      else if (cc <= 'Rain') 
      { 
       weatherCondition.ImageLocation = "rain.png"; 
      } 

      else if (cc <= 'Mostly Cloudy') 
      { 
       weatherCondition.ImageLocation = "mostlycloudy.png"; 
      } 
      else if (cc <= 'Rain') 
      { 
       weatherCondition.ImageLocation = "rain.png"; 
      } 
      else if (cc >= 'Scattered Thunderstorms') 
      { 
       weatherCondition.ImageLocation = "scatteredthunderstorms.png"; 
      } 


     } 

     private void button1_Click(object sender, EventArgs e) 
     { 


     } 

     private void button1_Click_1(object sender, EventArgs e) 
     { 
      Getweather(); 
      textBox1.Text = Town; 
      textBox3.Text = Condition; 
      //textBox4.Text = Humidity; 
      //textBox5.Text = Windspeed; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      database();  
     } 

     private void closeToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void openToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      this.WindowState = FormWindowState.Maximized; 
     } 

     private void refreshToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      database(); 
     } 

     private void tabPage1_Click(object sender, EventArgs e) 
     { 

     } 

     private void graph() 
     { 
      if (radioButton2.Checked) 
      { 
       MySqlDataReader MyReader2; 
       string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456"; 
       MySqlConnection MyConn2 = new MySqlConnection(MyConnection); 
       MyConn2.Open(); 
       string query2 = " SELECT * FROM London "; 
       MySqlCommand MyCommand2 = new MySqlCommand(query2, MyConn2); 
       MyReader2 = MyCommand2.ExecuteReader(); 

       while (MyReader2.Read()) 
       { 
        chart1.Series["Series1"].Points.AddXY(MyReader2.GetString("Time"), MyReader2.GetString("Temperature")); 
        chart1.Series["Series1"].ChartType = SeriesChartType.FastLine; 
        chart1.Series["Series1"].Color = Color.Blue; 
       } 
      } 
      if (radioButton1.Checked) 
      { 

       MySqlDataReader MyReader2; 
       string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456"; 
       MySqlConnection MyConn2 = new MySqlConnection(MyConnection); 
       MyConn2.Open(); 
       string query2 = " SELECT * FROM London "; 
       MySqlCommand MyCommand2 = new MySqlCommand(query2, MyConn2); 
       MyReader2 = MyCommand2.ExecuteReader(); 
       while (MyReader2.Read()) 
       { 
        chart1.Series["Series1"].Points.AddXY(MyReader2.GetString("Time"), MyReader2.GetString("Temperature2")); 
        chart1.Series["Series1"].ChartType = SeriesChartType.FastLine; 
        chart1.Series["Series1"].Color = Color.Blue; 
       } 
      } 

     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      graph(); 
      Getweather(); 
      database(); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      graph(); 
      Getweather(); 
      database(); 
     } 

     private void radioButton1_CheckedChanged(object sender, EventArgs e) 
     { 
      double celc = (Int32.Parse(Temperature) - 32)/(1.8); 
      string Temperature2 = celc.ToString(); 
      textBox2.Text = Temperature2; 
     } 

     private void radioButton2_CheckedChanged(object sender, EventArgs e) 
     { 
      textBox2.Text = Temperature; 
     } 

     private void textBox7_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      cityNumber = Int32.Parse(textBox7.Text); 
      timer1.Interval = Int32.Parse(textBox8.Text); 
     } 

     private void optionsToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

     } 

     private void tabPage3_Click(object sender, EventArgs e) 
     { 

     } 

     private void toolStripTextBox1_Click(object sender, EventArgs e) 
     { 
      Getweather(); 
      toolStripTextBox1.Text = "Temperature:" + Temperature + " F"; 
     } 

     private void weatherCondition_Click(object sender, EventArgs e) 
     { 

     } 


     } 
     } 

For database in localhost 
CREATE TABLE London 
(ID INT NOT NULL AUTO_INCREMENT, City VARCHAR(30) NOT NULL, Time  VARCHAR(20) NOT NULL, Temperature VARCHAR(5) NOT NULL, Temperature2 VARCHAR(5) NOT NULL, WeatherCondition VARCHAR(30) NOT NULL, PRIMARY KEY (ID)); 

回答

0

只能說雅虎天氣JSON格式。結果將在query.results.channel.item.forecast。查看以下示例,該示例使用Newtonsoft.Json進行JSON解析:

dynamic jsonData = JObject.Parse(responseFromServer); 
if (jsonData["query"].count == 0) 
{ 
    throw new Exception("Failed to obtain forecast from the server."); 
} 

var convertFromFtoC = jsonData["query"].results.channel.units.temperature == "F"; 

foreach (var v in jsonData["query"].results.channel.item.forecast) 
{ 
    var date = (DateTime)v.date; 
    var tempLo = (int)v.low; 
    var tempHi = (int)v.high; 

    if (convertFromFtoC) 
    { 
     tempLo = TemperatureConverter.FahrenheitToCelcius(tempLo); 
     tempHi = TemperatureConverter.FahrenheitToCelcius(tempHi); 
    } 

    dailyForecst.Add(new ForecastDataForTheDay { Date = date, TempLo = tempLo, TempHi = tempHi }); 
}