Friday, September 26, 2008

Read Weather Forecast From Indonesia's BMG in .NET code (C#)


If you want to know weather forecast in Indonesia, you can use the data from BMG (Badan Meteorologi dan Geofisika). BMG provides the data in two types, today and tomorrow's weather forecast. All data is saved in an XML file and located on http://www.bmg.go.id/dataxml/ . Today's weather forecast is provided on a file named cuaca_indo_1.xml , whereas tomorrow's forecast on cuaca_indo_2.xml . It's up to you to use which files.

Here are the sample code to read the XML files in .NET (C#)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
using System.Collections.Generic;

public partial class UC_AllWeather : System.Web.UI.UserControl
{
List<MyWeather> weathers = new List<MyWeather>();
protected void Page_Load(object sender, EventArgs e)
{
LoadBmgXml();
GridView1.DataSource = weathers;
GridView1.DataBind();
}
private void LoadBmgXml()
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load("http://www.bmg.go.id/dataxml/cuaca_indo_1.xml");
XPathNavigator root = doc.CreateNavigator();
XPathNodeIterator nodes = root.Select("/Cuaca/Isi/Row");
while (nodes.MoveNext())
{
string city = nodes.Current.SelectSingleNode("Kota").Value;
string weatherCondition = nodes.Current.SelectSingleNode("Cuaca").Value;
string minimumTemperature = nodes.Current.SelectSingleNode("SuhuMin").Value;
string maximumTemperature = nodes.Current.SelectSingleNode("SuhuMax").Value;
string minimumMoisture = nodes.Current.SelectSingleNode("KelembapanMin").Value;
string maximumMoisture = nodes.Current.SelectSingleNode("KelembapanMax").Value;
weathers.Add(new MyWeather(city,weatherCondition,minimumTemperature,maximumTemperature,minimumMoisture,maximumMoisture));

}
}
catch (Exception ex)
{

Response.Write(ex.Message);
}
}
public class MyWeather
{
private string _city;

public string City
{
get { return _city; }
set { _city = value; }
}

private string _weatherCondition;

public string WeatherCondition
{
get { return _weatherCondition; }
set { _weatherCondition = value; }
}

private string _minTemp;

public string MinimumTemperature
{
get { return _minTemp; }
set { _minTemp = value; }
}

private string _maxTemp;

public string MaximumTemperature
{
get { return _maxTemp; }
set { _maxTemp = value; }
}

private string _minMoisture;

public string MinimumMoisture
{
get { return _minMoisture; }
set { _minMoisture = value; }
}

private string _maxMoisture;

public string MaximumMoisture
{
get { return _maxMoisture; }
set { _maxMoisture = value; }
}




public MyWeather()
{

}
public MyWeather(string city,string weatherCondition,
string minimumTemperature,string maximumTemperature,
string minimumMoisture,string maximumMoisture)
{
this.City = city;
this.WeatherCondition = weatherCondition;
this.MinimumTemperature = minimumTemperature;
this.MaximumTemperature = maximumTemperature;
this.MinimumMoisture = minimumMoisture;
this.MaximumMoisture = maximumMoisture;
}
}
}

The idea reading the XML files provided by BMG actually comes from this blog . The article shows you how to read the XML in PHP, I modified it to .NET code (C#). All the credit of this goes to the original poster. Hope it helps.

No comments: