Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}
}
Posted
Updated 11-Oct-17 1:39am

1 solution

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];
 
Share this answer
 
v2
Comments
kobinath 11-Oct-17 8:28am    
private void button2_Click(object sender, EventArgs e)
{
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");

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();

var x = dataSet.ReadXml(sr);

dataGridView1.DataSource = x;

}
}


}
}


the result should be displayed on the dataGridView1. i wrote above but no result has been displayed please any one can fix this problem.
kobinath 11-Oct-17 11:00am    
dataGridView1.DataSource = dataSet.Tables[0];

when i write dataset getting an error while running the code(dataset does not exist in the current context)
TheRealSteveJudge 11-Oct-17 10:23am    
Please see updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900