Bonjour,
j'ai crée un code en c# (sur Windows Phone 7) qui affichera des données extraites d'un fichier xml, mais il ne fonctionne pas ou il m'affiche "NullReferenceException".
Voici le fichier xml:
<?xml version="1.0" encoding="utf-8" ?>
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_conditions>
<day_of_week data="lun."/>
<low data="28"/>
<high data="38"/>
<condition data="Partiellement ensoleillé"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="mar."/>
<low data="27"/>
<high data="39"/>
<condition data="Temps clair"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="mer."/>
<low data="25"/>
<high data="38"/>
<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="jeu."/>
<low data="24"/>
<high data="33"/>
<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
</weather>
et voici mon code:
namespace WEATHER2
{
public partial class MainPage : PhoneApplicationPage
{
// Constructeur
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
XDocument doc = XDocument.Load("Gweather.xml");
var x from c in doc.Root.Element("weather").Elements() where c.Name "forecast_conditions" select c;
foreach (XElement element in x)
{
Weather_Element welement = new Weather_Element();
welement.Day = element.Element("day_of_week").Attribute("data").Value;
welement.Low = element.Element("low").Attribute("data").Value;
welement.High = element.Element("high").Attribute("data").Value;
welement.Condition = element.Element("condition").Attribute("data").Value;
}
listBox1.ItemsSource = x;
}
public class Weather_Element
{
string day;
string low;
string high;
string condition;
public string Day
{
get { return day; }
set { day = value; }
}
public string Low
{
get { return low; }
set { low = value; }
}
public string High
{
get { return high; }
set { high = value; }
}
public string Condition
{
get { return condition; }
set { condition = value; }
}
}
}
}