Hello all, i have written a webservice that returns an xmlelement. The relevant part of the code is as follows...
[WebMethod(Description = "Get Current Weather Condition for a particular city")]
public XmlElement GetWeatherCondition(string strLocation)
{
strLocation += ".xml";
DataSet ds = new DataSet("Weather_Condition");
XmlDocument xmlConditions = new XmlDocument();
try
{
xmlConditions.Load(string.Format("http://api.wunderground.com/api/xxxxxxx/geolookup/conditions/q/np/{0}", strLocation));
if (xmlConditions.SelectSingleNode("/response/error") != null)
{
condition.Error = xmlConditions.SelectSingleNode("/response/error/description").InnerText;
}
else
{
condition.City = xmlConditions.SelectSingleNode("/response/location/city").InnerText;
condition.Condition = xmlConditions.SelectSingleNode("/response/current_observation/weather").InnerText;
.........
dtCurrent.Rows.Add(condition.City, DateTime.Now.DayOfWeek.ToString(), condition.Condition, condition.TempF, condition.TempC, condition.Humidity, condition.Wind, condition.Error, condition.IconUrl);
ds.Tables.Add(dtCurrent);
}
XmlDataDocument xDataDoc = new XmlDataDocument(ds);
XmlElement xDataElement = xDataDoc.DocumentElement;
return xDataElement;
}
The Code works gr8, but when there is no internet connection, then the xmlconditions.Load( url that returns the xml data) fails. Now what i want is if there is no internet connection then i want to return default values that's inside a different class(condition). How can i do that?? i wrote it in catch as follows
catch
{
dtCurrent.Rows.Add(condition.City, DateTime.Now.DayOfWeek.ToString(), condition.Condition, condition.TempF, condition.TempC, condition.Humidity, condition.Wind, condition.Error, condition.IconUrl);
ds.Tables.Add(dtCurrent);
}
but this breaks the execution of the entire application and webform does not load beacause of this single weather control.
Further improving the Question...i have called this function in a classlibrary...in the library i have a function as follows...
public Dictionary<string,string> LoadCurrentCondition(string strCity)
{
XmlElement xData = Weather.GetWeatherCondition(strCity);
i want to make changes in the GetWeatherCondtion() function so that it can return default values when there is no internet connection.
FYI, Condtion class is as follows,
public class Conditions
{
private string strCity = "No Data";
private string strConditions = "No Data";
private string strDayOfWeek = DateTime.Now.DayOfWeek.ToString();
private string strTempF = "No Data";
private string strMinTempF = "No Data";
private string strTempC = "No Data";
public string City
{
get
{
return this.strCity;
}
set
{
this.strCity = value;
}
}
public string Condition
{
get
{
return this.strConditions;
}
set
{
this.strConditions = value;
}
}
}
as you can see from the conditions class, i just need to get the properties values and return them...which is by default "No DATA" when there is no internet connection...but i'm not being able to do so.
Please help
Minghang