Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i want to make a simple weather forecasting system using c#. i need to display only cloud condition,temperature, humidity,wind only to display for coming up week for 7 days .please any help me to do this system.i attached the code I tried so far. the final 7-day result has to be displayed at the data grid view

What I have tried:

StringBuilder sb = new StringBuilder();
sb.Append("http://api.apixu.com/v1/forecast.xml?key=5[DELETED]0&q=");
sb.Append(txtcity.Text);
sb.Append("&days={1}");
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();
dataSet.ReadXml(sr);
dataGridView1.DataSource = dataSet.Tables[0];
}
}
Posted
Updated 19-Oct-17 0:47am
v2

I see at least 3 things wrong:
  • You've exposed your APIXU private key to the world by including it in your post.
  • The 2nd .Append() call should be .Append("&days=7").
  • You don't need the last .Append() call.
/ravi
 
Share this answer
 
Comments
kobinath 19-Oct-17 6:46am    
it will dislplayed only one day i want to display 7 days coming up week information in a grid can you write the code
Ravi Bhavnani 19-Oct-17 6:57am    
The URL http://api.apixu.com/v1/forecast.xml?key=[KEY]&q=Paris&days=7 returns 7 days for me.

/ravi
You are using a placeholder while building your string, but you may use it incorrectly.
Maybe try:
C#
int numberOfDays = 7;
StringBuilder sb = new StringBuilder();
sb.Append("http://api.apixu.com/v1/forecast.xml?key=<redacted>&q=");
sb.Append(txtcity.Text);
sb.AppendFormat("&days={0}", numberOfDays);

Depending on C# version you are using, you may also try:
C#
sb.Append($"&days={numberOfDays}");

(notice the $ sign in front of the string in the second version).
You could also group this into a single statement, eliminating the need to create a new StringBuilder everytime the method is called:
C#
string key = "<redacted>";
string city = txtcity.Text;
int numberOfDays = 7;
string requestText = $"http://api.apixu.com/v1/forecast.xml?key={key}&q={city}&days={numberOfDays}";

Finally, there seems to be a C# specific API that you could try to use:
apixu/apixu-csharp
[^]
Hope this helps. Kindly.
 
Share this answer
 
v4
Comments
kobinath 19-Oct-17 7:01am    
simple weather forecasting system using c#. i need to display only cloud condition,temperature, humidity,wind only to display for coming up week for 7 days. please write the code for me. above code doen't work please codeproject.
phil.o 19-Oct-17 7:11am    
Sorry, CodeProject is not a code-ordering service. I'm OK to help when there are some difficulties, but I will never do your work for you.
Since you seem to have a valid subscription to this service, I suggest you seek support from them. I personnaly never have used apixu service, nor do I plan to, at least in nearby future.
kobinath 19-Oct-17 11:45am    
string uri = string.Format("http://api.apixu.com/v1/forecast.xml?key=5742bec32f4141e08db171907171010&q={0}&days=7", city);
XDocument doc = XDocument.Load(uri);
foreach (var npc in doc.Descendants("forecastday"))
{
MessageBox.Show((string)npc.Descendants("date").FirstOrDefault());
MessageBox.Show("Max temp " + (string)npc.Descendants("maxtemp_c").FirstOrDefault());
MessageBox.Show("Min temp " + (string)npc.Descendants("mintemp_c").FirstOrDefault());
MessageBox.Show("Text " + (string)npc.Descendants("text").FirstOrDefault());
MessageBox.Show("Icon " + (string)npc.Descendants("http"+"icon").FirstOrDefault());

}
now working successfully. instead of message box how can put all data in to the data grid

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