Problème XML Parsing sur Windows Phone 7

MariamKh Messages postés 5 Date d'inscription jeudi 23 juin 2011 Statut Membre Dernière intervention 31 janvier 2012 - 14 juil. 2011 à 13:44
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 - 15 juil. 2011 à 15:34
Bonjour,
j'ai posé hier une question mais malgré vos propositions j'ai pas réussie à faire fonctionner mon application, j'ai donc essayé une autre méthode mais ça ne réussi pas non plus. Le problème c'est que mon fichier XML est un peu trop complexe , et que je ne voudrais afficher que quelques données :("forecast_conditions"/"day_of_week","low","high","condition").
XML:
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>
<city data="Tunis, Tunis"/>

<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2011-07-14"/>
<current_date_time data="2011-07-14 10:30:00 +0000"/>

</forecast_information>
<current_conditions>
<condition data="Couverture nuageuse partielle"/>
<temp_f data="90"/>
<temp_c data="32"/>
<humidity data="Humidité : 38 %"/>

<wind_condition data="Vent : NO à 23 km/h"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="jeu."/>
<low data="24"/>
<high data="33"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="ven."/>
<low data="21"/>
<high data="33"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="sam."/>
<low data="25"/>
<high data="34"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="dim."/>
<low data="25"/>
<high data="38"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
</weather>
</xml_api_reply>

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Xml;


namespace WeatherDev
    {
    public partial class MainPage : PhoneApplicationPage
        {
        public MainPage()
            {
            InitializeComponent();
            }

        private void button1_Click(object sender, RoutedEventArgs e)
            {
            string wk_url = "http://www.google.com/ig/api?weather=hammamet";
            Uri url = new Uri(wk_url, UriKind.Absolute);
            WebClient wkclient = new WebClient();
            wkclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wkclient_DownloadStringCompleted);
            wkclient.DownloadStringAsync(url);
            }
        void wkclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
            if (e.Error == null)
                {
                StringReader stream = new StringReader(e.Result);
                XmlReader reader = XmlReader.Create(stream);

                string jour = String.Empty;
                string low = String.Empty;
                string high = String.Empty;
                string condition = String.Empty;

                while (reader.Read())
                    {
                     if (reader.NodeType == XmlNodeType.Element)
                         {
                         if (reader.Name=="forecast_conditions")
                           {
                            if(reader.GetAttribute("day_of_week")=="data")
                             {
                             jour = reader.GetAttribute("data"); 
                             this.textBox1.Text = jour;
                            }

                           /* case "low":
                             {
                             low = reader.GetAttribute("data");
                             this.textBox2.Text = low;
                              case "high":
                             {
                             high = reader.GetAttribute("data");
                             this.textBox3.Text = high;
                             case "condition" :
                             {
                             condition = reader.GetAttribute("data");
                             this.textBox4.Text = condition;
                         }
                     }
                 }
             }
         }
        }
    }
                
            
        
    

3 réponses

MariamKh Messages postés 5 Date d'inscription jeudi 23 juin 2011 Statut Membre Dernière intervention 31 janvier 2012
14 juil. 2011 à 13:51
Bonjour,
je m'excuse pour les fautes, j'ai réécrit le code.
J'ai posé hier une question mais malgré vos propositions j'ai pas réussie à faire fonctionner mon application, j'ai donc essayé une autre méthode mais ça ne réussi pas non plus. Le problème c'est que mon fichier XML est un peu trop complexe , et que je ne voudrais afficher que quelques données :("forecast_conditions"/"day_of_week","low","high","condition").
XML:
Code XML :
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>
<city data="Tunis, Tunis"/>

<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2011-07-14"/>
<current_date_time data="2011-07-14 10:30:00 +0000"/>

</forecast_information>
<current_conditions>
<condition data="Couverture nuageuse partielle"/>
<temp_f data="90"/>
<temp_c data="32"/>
<humidity data="Humidité : 38 %"/>

<wind_condition data="Vent : NO à 23 km/h"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="jeu."/>
<low data="24"/>
<high data="33"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="ven."/>
<low data="21"/>
<high data="33"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="sam."/>
<low data="25"/>
<high data="34"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="dim."/>
<low data="25"/>
<high data="38"/>

<condition data="Ensoleillé dans l'ensemble"/>
</forecast_conditions>
</weather>
</xml_api_reply>


Code C# :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Xml;


namespace WeatherDev
    {
    public partial class MainPage : PhoneApplicationPage
        {
        public MainPage()
            {
            InitializeComponent();
            }

        private void button1_Click(object sender, RoutedEventArgs e)
            {
            string wk_url = "http://www.google.com/ig/api?weather=tunis";
            Uri url = new Uri(wk_url, UriKind.Absolute);
            WebClient wkclient = new WebClient();
            wkclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wkclient_DownloadStringCompleted);
            wkclient.DownloadStringAsync(url);
            }
        void wkclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
            if (e.Error == null)
                {
                StringReader stream = new StringReader(e.Result);
                XmlReader reader = XmlReader.Create(stream);

                string jour = String.Empty;
                string low = String.Empty;
                string high = String.Empty;
                string condition = String.Empty;

                while (reader.Read())
                    {
                     if (reader.NodeType == XmlNodeType.Element)
                         {
                        Switch (reader.Name)
                           {
                           Case "day_of_week" :
                             {
                             jour = reader.GetAttribute("data"); 
                             this.textBox1.Text = jour;
                            }break;

                          case "low":
                             {
                             low = reader.GetAttribute("data");
                             this.textBox2.Text = low;
                             }break;
                              case "high":
                             {
                             high = reader.GetAttribute("data");
                             this.textBox3.Text = high;
                              }break;
                             case "condition" :
                             {
                             condition = reader.GetAttribute("data");
                             this.textBox4.Text = condition;
                             }break;
                         }
                     }
                 }
             }
         }
        }
    }
0
krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 49
14 juil. 2011 à 17:37
Merci de répondre à ton ancien sujet et non de recréer un sujet à chaque fois que tu veux y répondre.

Krimog : while (!(succeed = try())) ;
- Nous ne sommes pas des décodeurs ambulants. Le style SMS est prohibé. -
0
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 103
15 juil. 2011 à 15:34
Krimog++
mais ça ne réussi pas non plus

Sais-tu pourquoi? Pourrais-tu nous en dire plus? As-tu testé en débugguant pour cerner le problème?

@+
Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
Rejoignez-nous