IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

lecture d'un fichier xml


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Par défaut lecture d'un fichier xml
    Bonjour,
    Je suis débutante dans ce domaine :p
    Mon prob est une exception qui ce declenche a cause d'une erreur non gérer .. c'est la lecture d'un fichier xml.. je ne sais pa ou est le problème

    voila le code source:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    XmlDocument doc = new XmlDocument();
    doc.Load("Fochier.xml");
    XmlNodeList nodQuestion = doc.GetElementsByTagName("Question");
    XmlNodeList node = nodQuestion[val].ChildNodes;
    ennonce = node.Item(0).InnerText;
    nb_choix = int.Parse(node.Item(1).InnerText);
    voila le fichier xml:

    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <QCM>
    <Duree></Duree>
    <NbQuestion>7</NbQuestion>
    <ListeQuestion>
    <Question>
    <Ennonce>Encnncer n1</Ennonce>
    <NbChoix>1</NbChoix>
    <ListeChoix>
    <Choix>
    <NbChoixJuste>2</NbChoixJuste>
    <Ch_choix>Choix 11</Ch_choix>
    <Etat>true</Etat>
    </Choix>
    </ListeChoix>
    </Question>
    </ListeQuestion>
    </QCM>

    Merci de m'aider.

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    A première vue c'est le fichier XML qui n'est pas valide, il manque la déclaration XML en début de fichier :

    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    <?xml version="1.0"?>

    Si ça ne règle pas le problème, poste le message d'erreur exact, sinon on ne peut pas savoir ce qui ne va pas...

  3. #3
    Membre habitué
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Par défaut
    oui, elle exite,

    et voila le msg de l'exception:

    System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.

  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Ca veut dire que tu essaies d'accéder à une méthode ou une propriété d'une variable qui vaut null. L'erreur se produit sur quelle ligne ?

  5. #5
    Membre habitué
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Par défaut
    voila le message complet de l'exception:

    System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.
    à WindowsFormsApplication1.Choix..ctor(Int32 id_quest, Int32 id_choix) dans C:\Documents and Settings\Abboura\Bureau\projetCshrep\projet_v3\projet_v3\Choix.cs:ligne 38
    à WindowsFormsApplication1.Question..ctor(Int32 val) dans C:\Documents and Settings\Abboura\Bureau\projetCshrep\projet_v3\projet_v3\Question.cs:ligne 44
    à WindowsFormsApplication1.QCM..ctor(Candidat C, Effectue E) dans C:\Documents and Settings\Abboura\Bureau\projetCshrep\projet_v3\projet_v3\QCM.cs:ligne 55
    à WindowsFormsApplication1.Form1.affichage_qcm(Object sender, EventArgs e) dans C:\Documents and Settings\Abboura\Bureau\projetCshrep\projet_v3\projet_v3\Form1.cs:ligne 177
    à System.Windows.Forms.Control.OnClick(EventArgs e)
    à System.Windows.Forms.Button.OnClick(EventArgs e)
    à System.Windows.Forms.Button.PerformClick()
    à System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
    à System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
    à System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
    à System.Windows.Forms.Control.PreProcessMessage(Message& msg)
    à System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
    à System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)


    ************** Assemblys chargés **************
    mscorlib
    Version de l'assembly*: 2.0.0.0
    Version Win32*: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase*: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    projet_v3
    Version de l'assembly*: 1.0.0.0
    Version Win32*: 1.0.0.0
    CodeBase*: file:///C:/Documents%20and%20Settings/Abboura/Bureau/projetCshrep/projet_v3/projet_v3/bin/Release/projet_v3.exe
    ----------------------------------------
    System.Windows.Forms
    Version de l'assembly*: 2.0.0.0
    Version Win32*: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase*: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
    Version de l'assembly*: 2.0.0.0
    Version Win32*: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase*: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
    Version de l'assembly*: 2.0.0.0
    Version Win32*: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase*: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Xml
    Version de l'assembly*: 2.0.0.0
    Version Win32*: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase*: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    mscorlib.resources
    Version de l'assembly*: 2.0.0.0
    Version Win32*: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase*: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    System.Windows.Forms.resources
    Version de l'assembly*: 2.0.0.0
    Version Win32*: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase*: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_fr_b77a5c561934e089/System.Windows.Forms.resources.dll
    ----------------------------------------

  6. #6
    Membre habitué
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Par défaut
    voila le fichier choix.cs


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
     
    namespace WindowsFormsApplication1
    {
     
        [Serializable]
        public class Choix
        {
            //ATTRIBUTS:
            private int nb_choix_juste;
            private String ch_choix;
            private bool etat = false;
     
            //METHODES:
            public Choix()
            {
            }
     
            public Choix(int id_quest,int id_choix)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("qcm.xml");
     
                XmlNodeList nodQuestion = doc.GetElementsByTagName("Question");
                XmlNodeList nodChoix = doc.GetElementsByTagName("Choix");
     
     
                foreach (XmlNode ele1 in nodQuestion[id_quest].ChildNodes)
                {
                    if (ele1.Name == "ListeChoix")
                        {
                            XmlNodeList ele2 = ele1.ChildNodes;
                            XmlNode node = ele2.Item(id_choix);
                            this.ch_choix = node.ChildNodes.Item(0).InnerText;
                            this.ch_choix = node.ChildNodes.Item(1).InnerText;
                            this.ch_choix = node.ChildNodes.Item(2).InnerText;
     
                        }
     
                }
            }
     
            //Accesseur au nb choix juste
            public int NbChoixJuste
            {
                get { return this.nb_choix_juste; }
                set
                {
                    if (value <= 0)
                    {
                        this.nb_choix_juste = value;
                    }
                    else
                        throw new Exception("nb_choix_juste invalide");
                }
            }//fin
     
            //Accesseur au choix
            public String Ch_choix {
            get { return this.ch_choix; }
            set {
                if (!value.Equals(""))
                    {
                    this.ch_choix = value;
            } else
            throw new Exception("Chaine vide");
            }
            }//fin
     
            //Accesseur etat de choix
            public bool Etat
            {
                get { return this.etat; }
                set
                {
                    if (value == true || value == false)
                    {
                        this.etat = value;
                    }
                    else
                        throw new Exception("Etat invalide: 1 c'est juste ou 0 c'est faux");
                }
            }//fin
     
            // Affichage Choix
            public override string ToString()
            {
                return this.ch_choix;
            }//fin
     
        }
    }

  7. #7
    Membre habitué
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Par défaut
    le fichier question.cs:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Xml;
    using System.ComponentModel;
     
    namespace WindowsFormsApplication1
    {
        [Serializable]
        public class Question
        {
            //ATTRIBUTS:
            private int nb_choix;
            private String ennonce;
            private BindingList <Choix> liste_choix = new BindingList<Choix>();
     
            //METHODES:
            public Question()
            {
     
            }
            public Question(int val)
            {
     
                XmlDocument doc = new XmlDocument();
                doc.Load("qcm.xml");
     
                XmlNodeList nodQuestion = doc.GetElementsByTagName("Question");
     
                XmlNodeList node = nodQuestion[val].ChildNodes;
     
                //initialisation  de nb choix
                this.ennonce = node.Item(0).InnerText;
     
                //initialisation  de nb choix
                this.nb_choix = int.Parse(node.Item(1).InnerText);     
     
     
                //initialisation  de liste choix
                for (int i = 1; i <= nb_choix; i++)
                {
                    this.liste_choix.Add(new Choix(val, i));
                }
            }//fin
     
            // Accesseur ennonce Question
            public String Ennonce
            {
                get { return this.ennonce; }
                set
                {
                    if (value.Trim().Length > 0)
                       this.ennonce = value;
                    else
                        throw new Exception("Ennoner question  invalide!!!");
                }
            }//fin
     
     
            // Accesseur Nombre Question
            public int NbChoix
            {
                get { return this.nb_choix; }
                set
                {
                    if (value > 0)
                        this.nb_choix = value;
                    else
                        throw new Exception("Nombre question  invalide!!!");
                }
            }//fin
     
            //Accesseur Liste des choix
            public BindingList <Choix> ListeChoix
            {
                get { return this.liste_choix; }
                set { this.liste_choix = value; }
            }//fin
     
            // Ajouter un choix
            public void AjouterChoix(Choix c)
            {
                if (c != null)
                    this.liste_choix.Add(c);
                else
                    throw new ArgumentNullException("Ajout d'une choix non initialisée.");
            }//fin
     
            // Suppression d'une choix
            public void SupprimerChoix(Choix c)
            {
                if (c != null)
                    this.liste_choix.Remove(c);
                else
                    throw new ArgumentNullException("Suppression d'une choix non initialisée.");
            }//fin
     
     
            // Trouver une choix
            public Choix TrouverChoix(String choix)
            {
                foreach (Choix c in this.liste_choix)
                {
                    if (c.Ch_choix.Equals(choix) )
                        return c;
                }
                return null;
            }//fin
     
            // Affichage question
            public override string ToString()
            {
                return this.ennonce;
            }//fin
     
        }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de lecture d'un fichier XML d'InfoPath
    Par snadus dans le forum InfoPath
    Réponses: 7
    Dernier message: 06/06/2006, 14h45
  2. Lecture d'un fichier XML de config avec Java
    Par cofy dans le forum Format d'échange (XML, JSON...)
    Réponses: 6
    Dernier message: 31/01/2006, 15h54
  3. Problème de lecture dans un fichier xml
    Par Pyra dans le forum Langage
    Réponses: 2
    Dernier message: 18/12/2005, 00h13
  4. Probleme de lecture d'un fichier XML
    Par chleuh dans le forum Langage
    Réponses: 8
    Dernier message: 30/08/2005, 12h04
  5. [C#] [XML] Lecture d'un fichier XML => Combobox
    Par Chad`Chiwa dans le forum Windows Forms
    Réponses: 8
    Dernier message: 09/03/2005, 08h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo