C#中的Weather API预测系统 [英] Weather API forecast system in C#

查看:108
本文介绍了C#中的Weather API预测系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望显示7周的时间。天气信息这是API,我曾经忘记7天伦敦的结果



http://api.apixu.com/v1/forecast.xml?key=5742bec32f4141e08db171907171010& ; q = india& days = 7

如果我在c#中编写代码我应该没问题



StringBuilder sb = new StringBuilder( );

sb.Append(http://api.apixu.com/v1/forecast.xml?key=5742bec32f4141e08db171907171010&q=);

sb.Append(txtbox.Text);

sb.Append(& days =);

sb.Append(7);

这是我编写的代码,用于显示在数据网格视图上,但没有结果显示任何人可以修复代码供我显示7天天气信息将显示在数据网格视图上。我写了我试过的完整代码。



我尝试了什么:



StringBuilder sb = new StringBuilder();

sb.Append(http://api.apixu.com/v1/forecast.xml?key=5742bec32f4141e08db171907171010&q=);

sb.Append(txtcity.Text);

sb.Append(& days =);

sb.Append( 7);









XmlReader xmlFile;

xmlFile = XmlReader.Create(sb.ToString());

DataSet ds = new DataSet();

ds.ReadXml(xmlFile);



foreach(ds.Tables中的DataTable表)

{

foreach(table.Rows中的DataRow行)

{

dataGridView1.DataSource = row.ItemArray;

}

}

解方案

看来你调用这个web服务的方法不正确。



你可以像这样获取数据。



 var request = sb.ToString(); 

使用(var webClient = new WebClient())
{
var response = webClient.DownloadData(request);

var xml = Encoding.UTF8.GetString(response);

using(var sr = new StringReader(xml))
{
var dataSet = new DataSet();

dataSet.ReadXml(sr);
}
}





更多信息:



DataSet本身包含8个表格。



位置,

当前,

条件,

预测,
预测日,

天,

astro,

小时


对于每个表,您必须有一个DataGridView。



例如位置可以这样显示:



 dataGridView1.DataSource = dataSet.Tables [0]; 


I want to display 7 days of the weeks. weather information this is API I used to forgetting 7 days result of London

http://api.apixu.com/v1/forecast.xml?key=5742bec32f4141e08db171907171010 &q=india&days=7
if I write code in c# below I should be no problem

StringBuilder sb = new StringBuilder();
sb.Append("http://api.apixu.com/v1/forecast.xml?key=5742bec32f4141e08db171907171010&q=");
sb.Append(txtbox.Text);
sb.Append("&days=");
sb.Append("7");
this is code i written to display on data grid view but no result display can any one fix the code for me to display 7 days weather information will display on data grid view. i wrote the full code which i have tried.

What I have tried:

StringBuilder sb = new StringBuilder();
sb.Append("http://api.apixu.com/v1/forecast.xml?key=5742bec32f4141e08db171907171010&q=");
sb.Append(txtcity.Text);
sb.Append("&days=");
sb.Append("7");




XmlReader xmlFile;
xmlFile = XmlReader.Create(sb.ToString());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);

foreach (DataTable table in ds.Tables)
{
foreach (DataRow row in table.Rows)
{
dataGridView1.DataSource = row.ItemArray;
}
}

解决方案

It seems that your approach of calling this web service is not correct.

You could fetch the data like this.

var request = sb.ToString();

using (var webClient = new WebClient())
{
        var response = webClient.DownloadData(request);

        var xml = Encoding.UTF8.GetString(response);

        using(var sr = new  StringReader(xml))
        {
            var dataSet = new DataSet();

            dataSet.ReadXml(sr);
        }
}



Some more info:

The DataSet itself contains 8 tables.

location,
current,
condition,
forecast,
forecastday,
day,
astro,
hour

For each of these tables you must have one DataGridView.

e.g. location can be shown like this:

dataGridView1.DataSource = dataSet.Tables[0];


这篇关于C#中的Weather API预测系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆