如何使用yahoo Yql查询使用woeid号收集天气信息
How to use yahoo Yql query to gather weather information using woeid number
我有一个文本框,我希望用户在其中输入他们的 woeid 号码,但我不确定如何将其添加到查询字符串中,在下面的代码中我可以获得洛杉矶的天气,但我想要的是现在是通过用户提供的woeid号码获取。
try
{
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='Los Angeles')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
var wData = new XmlDocument();
wData.Load(query);
var man = new XmlNamespaceManager(wData.NameTable);
man.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("query").SelectSingleNode("results").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("query/results/channel");
MainForm.WindSpeed = channel.SelectSingleNode("yweather:wind", man).Attributes["speed"].Value;
MainForm.Town = channel.SelectSingleNode("yweather:location", man).Attributes["city"].Value;
MainForm.Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["temp"].Value;
MainForm.Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["text"].Value;
MainForm.Humidity = channel.SelectSingleNode("yweather:atmosphere", man).Attributes["humidity"].Value;
MainForm.TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["text"].Value;
MainForm.TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["high"].Value;
MainForm.TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["low"].Value;
}
catch {}
}
尝试 xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string city = "Los Angeles";
string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", city);
XDocument wData = XDocument.Load(query);
XNamespace ns = wData.Root.GetDefaultNamespace();
XElement xWind = wData.Descendants().Where(x => x.Name.LocalName == "wind").FirstOrDefault();
int speed = (int)xWind.Attribute("speed");
XElement xLocation = wData.Descendants().Where(x => x.Name.LocalName == "location").FirstOrDefault();
string town = (string)xLocation.Attribute("city");
XElement xCondition = wData.Descendants().Where(x => x.Name.LocalName == "condition").FirstOrDefault();
int temp = (int)xCondition.Attribute("temp");
XElement xAtmosphere = wData.Descendants().Where(x => x.Name.LocalName == "atmosphere").FirstOrDefault();
int humidity = (int)xAtmosphere.Attribute("humidity");
List<XElement> xForecast = wData.Descendants().Where(x => x.Name.LocalName == "forecast").ToList(); ;
string tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
int high = (int)xForecast.FirstOrDefault().Attribute("high");
int low = (int)xForecast.FirstOrDefault().Attribute("low");
}
}
}
添加行
var woeid = Request.QueryString["woeid"];
然后像这样将其包含在您现有的查询中:
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid = '{0}'&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", woeid);
我喜欢下面的解决方案,它处理同名的多个城市
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Threading;
namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string[] cities = { "Los Angeles", "New York", "Salem", "Portland", "Washington" };
DataTable dt = GetWeather(cities);
dataGridView1.DataSource = dt;
}
DataTable GetWeather(string[] cities)
{
DataTable dt = new DataTable();
dt.Columns.Add("City", typeof(string));
dt.Columns.Add("Region", typeof(string));
dt.Columns.Add("Humidity", typeof(int));
dt.Columns.Add("Wind Speed", typeof(int));
dt.Columns.Add("Temperature", typeof(int));
dt.Columns.Add("Condition", typeof(string));
dt.Columns.Add("High Temperature", typeof(int));
dt.Columns.Add("Low Temperature", typeof(int));
foreach (string inputCity in cities)
{
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(100) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", inputCity);
XDocument wData = XDocument.Load(query);
XNamespace ns = wData.Root.GetDefaultNamespace();
//System.Threading.Thread.Sleep(10000);
foreach (XElement channel in wData.Descendants().Where(x => x.Name.LocalName == "channel"))
{
string city = "";
string region = "";
int? humidity = null;
int? speed = null;
int? temp = null;
string tfCond = "";
int? high = null;
int? low = null;
XNamespace yWeatherNs = channel.Elements().First().GetNamespaceOfPrefix("yweather");
XElement xLocation = channel.Element(yWeatherNs + "location");
if (xLocation == null)
{
continue;
}
else
{
city = (string)xLocation.Attribute("city");
region = (string)xLocation.Attribute("region");
}
XElement xAtmosphere = channel.Element(yWeatherNs + "atmosphere");
if (xAtmosphere != null)
{
humidity = (int)xAtmosphere.Attribute("humidity");
}
XElement xWind = channel.Element(yWeatherNs + "wind");
if (xWind != null)
{
speed = (int)xWind.Attribute("speed");
}
XElement item = channel.Element("item");
if (item != null)
{
XElement xCondition = item.Element(yWeatherNs + "condition");
if (xCondition != null)
{
temp = (int)xCondition.Attribute("temp");
}
List<XElement> xForecast = item.Elements(yWeatherNs + "forecast").ToList(); ;
if (xForecast != null)
{
tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
high = (int)xForecast.FirstOrDefault().Attribute("high");
low = (int)xForecast.FirstOrDefault().Attribute("low");
}
}
dt.Rows.Add(new object[] { city, region, humidity, speed, temp, tfCond, high, low });
}
}
dt = dt.AsEnumerable().OrderBy(x => x.Field<string>("City")).ThenBy(x => x.Field<string>("Region")).CopyToDataTable();
return dt;
}
}
}
我有一个文本框,我希望用户在其中输入他们的 woeid 号码,但我不确定如何将其添加到查询字符串中,在下面的代码中我可以获得洛杉矶的天气,但我想要的是现在是通过用户提供的woeid号码获取。
try
{
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='Los Angeles')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
var wData = new XmlDocument();
wData.Load(query);
var man = new XmlNamespaceManager(wData.NameTable);
man.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("query").SelectSingleNode("results").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("query/results/channel");
MainForm.WindSpeed = channel.SelectSingleNode("yweather:wind", man).Attributes["speed"].Value;
MainForm.Town = channel.SelectSingleNode("yweather:location", man).Attributes["city"].Value;
MainForm.Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["temp"].Value;
MainForm.Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["text"].Value;
MainForm.Humidity = channel.SelectSingleNode("yweather:atmosphere", man).Attributes["humidity"].Value;
MainForm.TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["text"].Value;
MainForm.TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["high"].Value;
MainForm.TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["low"].Value;
}
catch {}
}
尝试 xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string city = "Los Angeles";
string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", city);
XDocument wData = XDocument.Load(query);
XNamespace ns = wData.Root.GetDefaultNamespace();
XElement xWind = wData.Descendants().Where(x => x.Name.LocalName == "wind").FirstOrDefault();
int speed = (int)xWind.Attribute("speed");
XElement xLocation = wData.Descendants().Where(x => x.Name.LocalName == "location").FirstOrDefault();
string town = (string)xLocation.Attribute("city");
XElement xCondition = wData.Descendants().Where(x => x.Name.LocalName == "condition").FirstOrDefault();
int temp = (int)xCondition.Attribute("temp");
XElement xAtmosphere = wData.Descendants().Where(x => x.Name.LocalName == "atmosphere").FirstOrDefault();
int humidity = (int)xAtmosphere.Attribute("humidity");
List<XElement> xForecast = wData.Descendants().Where(x => x.Name.LocalName == "forecast").ToList(); ;
string tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
int high = (int)xForecast.FirstOrDefault().Attribute("high");
int low = (int)xForecast.FirstOrDefault().Attribute("low");
}
}
}
添加行
var woeid = Request.QueryString["woeid"];
然后像这样将其包含在您现有的查询中:
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid = '{0}'&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", woeid);
我喜欢下面的解决方案,它处理同名的多个城市
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Threading;
namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string[] cities = { "Los Angeles", "New York", "Salem", "Portland", "Washington" };
DataTable dt = GetWeather(cities);
dataGridView1.DataSource = dt;
}
DataTable GetWeather(string[] cities)
{
DataTable dt = new DataTable();
dt.Columns.Add("City", typeof(string));
dt.Columns.Add("Region", typeof(string));
dt.Columns.Add("Humidity", typeof(int));
dt.Columns.Add("Wind Speed", typeof(int));
dt.Columns.Add("Temperature", typeof(int));
dt.Columns.Add("Condition", typeof(string));
dt.Columns.Add("High Temperature", typeof(int));
dt.Columns.Add("Low Temperature", typeof(int));
foreach (string inputCity in cities)
{
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(100) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", inputCity);
XDocument wData = XDocument.Load(query);
XNamespace ns = wData.Root.GetDefaultNamespace();
//System.Threading.Thread.Sleep(10000);
foreach (XElement channel in wData.Descendants().Where(x => x.Name.LocalName == "channel"))
{
string city = "";
string region = "";
int? humidity = null;
int? speed = null;
int? temp = null;
string tfCond = "";
int? high = null;
int? low = null;
XNamespace yWeatherNs = channel.Elements().First().GetNamespaceOfPrefix("yweather");
XElement xLocation = channel.Element(yWeatherNs + "location");
if (xLocation == null)
{
continue;
}
else
{
city = (string)xLocation.Attribute("city");
region = (string)xLocation.Attribute("region");
}
XElement xAtmosphere = channel.Element(yWeatherNs + "atmosphere");
if (xAtmosphere != null)
{
humidity = (int)xAtmosphere.Attribute("humidity");
}
XElement xWind = channel.Element(yWeatherNs + "wind");
if (xWind != null)
{
speed = (int)xWind.Attribute("speed");
}
XElement item = channel.Element("item");
if (item != null)
{
XElement xCondition = item.Element(yWeatherNs + "condition");
if (xCondition != null)
{
temp = (int)xCondition.Attribute("temp");
}
List<XElement> xForecast = item.Elements(yWeatherNs + "forecast").ToList(); ;
if (xForecast != null)
{
tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
high = (int)xForecast.FirstOrDefault().Attribute("high");
low = (int)xForecast.FirstOrDefault().Attribute("low");
}
}
dt.Rows.Add(new object[] { city, region, humidity, speed, temp, tfCond, high, low });
}
}
dt = dt.AsEnumerable().OrderBy(x => x.Field<string>("City")).ThenBy(x => x.Field<string>("Region")).CopyToDataTable();
return dt;
}
}
}