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 :

Problème de lecture d'un fichier XML


Sujet :

C#

  1. #1
    Nouveau membre du Club
    Inscrit en
    Juin 2008
    Messages
    30
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 30
    Points : 27
    Points
    27
    Par défaut Problème de lecture d'un fichier XML
    bonjour
    je travaille sur une fonction qui lit des données stockés dans un fichier XML, il s'agit des coordonnées des points qui regroupe des régions .

    alors j'ai le xml suivant :
    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
    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
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    <?xml version="1.0" encoding="UTF-8"?>
    <!--Sweep Lists-->
    <Sweep1>
      <Region ID="0">
        <point>9,10</point>
        <point>10,10</point>
        <point>11,10</point>
        <point>9,11</point>
        <point>10,11</point>
        <point>11,11</point>
        <point>9,12</point>
        <point>10,12</point>
        <point>11,12</point>
      </Region>
      <Region ID="1">
        <point>12,14</point>
        <point>13,14</point>
        <point>14,14</point>
        <point>12,15</point>
        <point>13,15</point>
        <point>14,15</point>
        <point>12,16</point>
        <point>13,16</point>
        <point>14,16</point>
      </Region>
      <Region ID="2">
        <point>20,10</point>
        <point>21,10</point>
        <point>22,10</point>
        <point>20,11</point>
        <point>21,11</point>
        <point>22,11</point>
        <point>20,12</point>
        <point>21,12</point>
        <point>22,12</point>
      </Region>
      <Region ID="3">
        <point>20,13</point>
        <point>21,13</point>
        <point>22,13</point>
        <point>20,14</point>
        <point>21,14</point>
        <point>22,14</point>
        <point>20,15</point>
        <point>21,15</point>
        <point>22,15</point>
      </Region>
      <Region ID="4">
        <point>26,11</point>
        <point>27,11</point>
        <point>28,11</point>
        <point>26,12</point>
        <point>27,12</point>
        <point>28,12</point>
        <point>26,13</point>
        <point>27,13</point>
        <point>28,13</point>
      </Region>
      <Region ID="5">
        <point>25,17</point>
        <point>26,17</point>
        <point>27,17</point>
        <point>25,18</point>
        <point>26,18</point>
        <point>27,18</point>
        <point>25,19</point>
        <point>26,19</point>
        <point>27,19</point>
      </Region>
      <Region ID="6">
        <point>31,18</point>
        <point>32,18</point>
        <point>33,18</point>
        <point>31,19</point>
        <point>32,19</point>
        <point>33,19</point>
        <point>31,20</point>
        <point>32,20</point>
        <point>33,20</point>
      </Region>
      <Region ID="7">
        <point>31,17</point>
        <point>32,17</point>
        <point>33,17</point>
        <point>34,17</point>
        <point>35,17</point>
        <point>31,18</point>
        <point>32,18</point>
        <point>33,18</point>
        <point>34,18</point>
        <point>35,18</point>
        <point>31,19</point>
        <point>32,19</point>
        <point>33,19</point>
        <point>34,19</point>
        <point>35,19</point>
        <point>31,20</point>
        <point>32,20</point>
        <point>33,20</point>
        <point>34,20</point>
        <point>35,20</point>
        <point>31,21</point>
        <point>32,21</point>
        <point>33,21</point>
        <point>34,21</point>
        <point>35,21</point>
      </Region>
      <Region ID="8">
        <point>38,22</point>
        <point>39,22</point>
        <point>40,22</point>
        <point>41,22</point>
        <point>42,22</point>
        <point>43,22</point>
        <point>44,22</point>
        <point>38,23</point>
        <point>39,23</point>
        <point>40,23</point>
        <point>41,23</point>
        <point>42,23</point>
        <point>43,23</point>
        <point>44,23</point>
        <point>38,24</point>
        <point>39,24</point>
        <point>40,24</point>
        <point>41,24</point>
        <point>42,24</point>
        <point>43,24</point>
        <point>44,24</point>
        <point>38,25</point>
        <point>39,25</point>
        <point>40,25</point>
        <point>41,25</point>
        <point>42,25</point>
        <point>43,25</point>
        <point>44,25</point>
        <point>38,26</point>
        <point>39,26</point>
        <point>40,26</point>
        <point>41,26</point>
        <point>42,26</point>
        <point>43,26</point>
        <point>44,26</point>
        <point>38,27</point>
        <point>39,27</point>
        <point>40,27</point>
        <point>41,27</point>
        <point>42,27</point>
        <point>43,27</point>
        <point>44,27</point>
      </Region>
      <Region ID="9">
        <point>44,22</point>
        <point>45,22</point>
        <point>46,22</point>
        <point>47,22</point>
        <point>48,22</point>
        <point>49,22</point>
        <point>50,22</point>
        <point>44,23</point>
        <point>45,23</point>
        <point>46,23</point>
        <point>47,23</point>
        <point>48,23</point>
        <point>49,23</point>
        <point>50,23</point>
        <point>44,24</point>
        <point>45,24</point>
        <point>46,24</point>
        <point>47,24</point>
        <point>48,24</point>
        <point>49,24</point>
        <point>50,24</point>
        <point>44,25</point>
        <point>45,25</point>
        <point>46,25</point>
        <point>47,25</point>
        <point>48,25</point>
        <point>49,25</point>
        <point>50,25</point>
        <point>44,26</point>
        <point>45,26</point>
        <point>46,26</point>
        <point>47,26</point>
        <point>48,26</point>
        <point>49,26</point>
        <point>50,26</point>
        <point>44,27</point>
        <point>45,27</point>
        <point>46,27</point>
        <point>47,27</point>
        <point>48,27</point>
        <point>49,27</point>
        <point>50,27</point>
      </Region>
    </Sweep1>

    alors je veux lire les cordonnées de tous les points situer dans tous les regions , alors j'ai essayé de faire cette fonction :
    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
     
    public Collection<Collection<Point>> ReadXmlFile()
            {
                XPathDocument docxml = new XPathDocument(@"C:\Users\Yamen\Desktop\cam1.xml");
                XPathNavigator lir = docxml.CreateNavigator();
                XPathNodeIterator iterateur = lir.Select("Sweep1/Region");
                Collection<Collection<Point>> listeB1 = new Collection<Collection<Point>>();
               Collection<Point> region = new Collection<Point>();
     while (iterateur.MoveNext())
                {
                    string s =iterateur.Current.SelectSingleNode("point").Value;
                    string[] coords = s.Split(',');
                    Point point = new Point(int.Parse(coords[0]), int.Parse(coords[1]));
                    region.Add(point);
                    listeB1.Add(region);     
                }
     
                return listeB1;
            }
    alors le resultat:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    < < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} >, < {X=9,Y=10}, {X=12,Y=14}, {X=20,Y=10}, {X=20,Y=13}, {X=26,Y=11}, {X=25,Y=17}, {X=31,Y=18}, {X=31,Y=17}, {X=38,Y=22}, {X=44,Y=22} > >
    là il prend le premier point de chaque region et les stocke dans la collection et ajoute cette region 10 fois (le nbre des region dans l'xml) dans la collection ListeB1!!!

    alors j'ai essayé de modifier la méthode en ajoutant un autre iterateur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     while (iterateur.MoveNext())
                {
                    XPathNodeIterator iterateurX = lir.Select("Sweep1/Region/point");
                    while (iterateurX.MoveNext())
                    {
                        string s = iterateurX.Current.Value;
                        string[] coords = s.Split(',');
                        Point point = new Point(int.Parse(coords[0]), int.Parse(coords[1]));
                        region.Add(point);
     
                    }
                    listeB1.Add(region);
                }
    le resultat :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    < < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... >, < {X=9,Y=10}, {X=10,Y=10}, {X=11,Y=10}, {X=9,Y=11}, {X=10,Y=11}, {X=11,Y=11}, {X=9,Y=12}, {X=10,Y=12}, {X=11,Y=12}, {X=12,Y=14}... > >
    là il prend tous les points de la première région et les répètes 10 fois (nbre des régions) !!!! et c pas se que je voudrais!!!!
    et merci d'avance pour l'aide

  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 : 42
    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
    Points : 39 749
    Points
    39 749
    Par défaut
    Utilise plutôt Linq to XML, ce sera plus simple...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
        var doc = XDocument.Load(@"C:\Users\Yamen\Desktop\cam1.xml");
        var query =
            from r in doc.Root.Elements("Region")
            select new
            {
                ID = (int)r.Attribute("ID"),
                Points = r.Elements("point").Select(p => ParsePoint(p.Value)).ToList()
            };
        var regions = query.ToList();

  3. #3
    Nouveau membre du Club
    Inscrit en
    Juin 2008
    Messages
    30
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 30
    Points : 27
    Points
    27
    Par défaut
    merciiiii pr votre réponse ça m'a bcp aidé

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

Discussions similaires

  1. [JDOM] Problème de lecture d'un fichier XML avec Java
    Par thonath dans le forum Format d'échange (XML, JSON...)
    Réponses: 7
    Dernier message: 27/05/2014, 11h52
  2. Problème de lecture d'un fichier XML
    Par Cyborg289 dans le forum VB.NET
    Réponses: 11
    Dernier message: 11/08/2010, 16h47
  3. problème de lecture d'un fichier xml spécifique
    Par f-mory dans le forum Développement de jobs
    Réponses: 0
    Dernier message: 23/10/2009, 15h53
  4. 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
  5. Problème de lecture dans un fichier xml
    Par Pyra dans le forum Langage
    Réponses: 2
    Dernier message: 18/12/2005, 00h13

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