我有一个文本框,我希望用户输入他们的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 {}
}
答案 0 :(得分:0)
尝试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");
}
}
}
答案 1 :(得分:0)
添加行
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);
答案 2 :(得分:0)
我喜欢下面的解决方案,该解决方案可以处理多个同名城市
import UIKit
struct HeadLine {
var date: Date
var title: String
var text: String
var image: String
}
struct TableSection<SectionItem: Comparable&Hashable, RowItem>: Comparable {
var sectionItem: SectionItem
var rowItems: [RowItem]
static func < (lhs: TableSection, rhs: TableSection) -> Bool {
return lhs.sectionItem > rhs.sectionItem
}
static func == (lhs: TableSection, rhs: TableSection) -> Bool {
return lhs.sectionItem == rhs.sectionItem
}
static func group(rowItems : [RowItem], by criteria : (RowItem) -> SectionItem ) -> [TableSection<SectionItem, RowItem>] {
let groups = Dictionary(grouping: rowItems, by: criteria)
return groups.map(TableSection.init(sectionItem:rowItems:)).sorted()
}
}
fileprivate func parseDate(_ str: String) -> Date {
let dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
return dateFormat.date(from: str)!
}
fileprivate func firstDayOfMonth(date: Date) -> Date {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)
return calendar.date(from: components)!
}
class testSectionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var testTableView: UITableView!
var headlines = [HeadLine]()
var sections = [TableSection<Date, HeadLine>]()
override func viewDidLoad() {
super.viewDidLoad()
testTableView.delegate = self
testTableView.dataSource = self
headlines = [
HeadLine(date: parseDate("2018-7-21"), title: "title1", text: "aaaaaaaaaaaaaaaa", image: "weight"),
HeadLine(date: parseDate("2018-5-22"), title: "title2", text: "bbbbbbbbbbbbbbbb", image: "user"),
HeadLine(date: parseDate("2017-5-23"), title: "title3", text: "cccccccccccccccc", image: "lock"),
HeadLine(date: parseDate("2018-5-24"), title: "title4", text: "dddddddddddddddd", image: "manager"),
HeadLine(date: parseDate("2018-5-25"), title: "title5", text: "eeeeeeeeeeeeeeee", image: "oka"),
HeadLine(date: parseDate("2018-5-26"), title: "title6", text: "ffffffffffffffff", image: "omura"),
]
self.sections = TableSection.group(rowItems: self.headlines, by: { (headline) in
firstDayOfMonth(date: headline.date)
})
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let section = self.sections[section]
let date = section.sectionItem
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = " yyyy/MM/dd"
return dateFormatter.string(from: date)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let section = self.sections[section]
return section.rowItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
let headline = self.headlines[indexPath.item]
cell.textLabel?.text = headline.title
cell.detailTextLabel?.text = headline.text
cell.imageView?.image = UIImage(named: headline.image)
return cell
}
}