Bonjour,

J'aimerais convertir un fichier csv de factures en fichier xml,

Voici la structure de ce fichier csv:

numeroFactureSaisi;dateFacture;deviseFacture;typeFacture;typeTva;
numeroMarche;numeroBonCommande;numeroFactureOrigine;modePaiement;lignePosteNumero;
lignePosteReference;lignePosteDenomination;lignePosteQunatite;lignePosteUnite;lignePosteMontantUnitaireHT;
lignePosteMontantRemiseHT;lignePosteTauxTva;ligneTvaTaux;ligneTvaMontantBaseHtParTaux;ligneTvaMontantTvaParTaux;
montantHtTotal;montantTVA;montantTtcTotal;montantRemiseGlobalTTC;montantAPayer;
commentaire

Voici un exemple de ce fichier csv:

49;2625017600****;SERVICE;54 208 370 *** ***;A1_FACTURE_FOURNISSEUR;49500****;03/03/2014;EUR;FACTURE;TVA_SUR_DEBIT;250562117****;BON N°2;0;VIREMENT;2;FRAISGAO;FRAIS D'EXPEDITION;1;Pièce;22,58;0;2;2;22,58;4,43;1529,69;87,32;1617,01;0;1617,01;
49;2625017600****;SERVICE;54 208 370 *** ***;A1_FACTURE_FOURNISSEUR;49500****;06/04/2017;EUR;FACTURE;TVA_SUR_DEBIT;250562117****;BON N°3;0;VIREMENT;1;TR39N50;ORTHESE SANS ARMATURE BIVALVE;1;Pièce;924,38;0;1;1;1029,74;56,64;1029,74;56,64;1086,38;0;1086,38;
49;2625017600****;SERVICE;54 208 370 *** ***;A1_FACTURE_FOURNISSEUR;49500****;06/04/2017;EUR;FACTURE;TVA_SUR_DEBIT;250562117****;BON N°4;0;VIREMENT;2;T39P01;MOULAGE TORSE;1;Pièce;105,36;0;1;1;1029,74;56,64;1029,74;56,64;1086,38;0;1086,38;
49;2625017600****;SERVICE;54 208 370 *** ***;A1_FACTURE_FOURNISSEUR;49500****;12/07/2017;EUR;FACTURE;TVA_SUR_DEBIT;250562117****;BON N°5;0;VIREMENT;2;TR39N50;ORTHESE SANS ARMATURE BIVALVE;1;Pièce;924,38;0;1;1;1029,74;56,64;1029,74;56,64;1086,38;0;1086,38;Commentaires
49;2625017600****;SERVICE;54 208 370 *** ***;A1_FACTURE_FOURNISSEUR;49500****;12/07/2017;EUR;FACTURE;TVA_SUR_DEBIT;250562117****;BON N°6;0;VIREMENT;3;T39P01;MOULAGE TORSE;1;Pièce;105,36;0;1;1;1029,74;56,64;1029,74;56,64;1086,38;0;1086,38;Commentaires
49;2625017600****;SERVICE;54 208 370 *** ***;A1_FACTURE_FOURNISSEUR;49500****;12/07/2017;EUR;FACTURE;TVA_SUR_DEBIT;250562117****;BON N°7;0;VIREMENT;1;TR39N50;ORTHESE SANS ARMATURE BIVALVE;1;Pièce;924,38;0;1;1;1029,74;56,64;1029,74;56,64;1086,38;0;1086,38;
49;2625017600****;SERVICE;54 208 370 *** ***;A1_FACTURE_FOURNISSEUR;49500****;12/07/2017;EUR;FACTURE;TVA_SUR_DEBIT;250562117****;BON N°8;0;VIREMENT;2;T39P01;MOULAGE TORSE;1;Pièce;105,36;0;1;1;1029,74;56,64;1029,74;56,64;1086,38;0;1086,38;

J'arrive à le convertir de cette façon:

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
//Lecture dans un tableau de string
            string[] source = File.ReadAllLines("Exemple Fichier.csv");
            XElement fac = new XElement("Root",
                from str in source
                let fields = str.Split(';')
                select new XElement("Facture",
                    new XElement("Site", fields[0]),
                    new XElement("codeDestinataire", fields[1]),
                    new XElement("codeServiceExecutant", fields[2]),
                    new XElement("idFournisseur", fields[3]),
                    new XElement("codeCadreFacturation", fields[4]),
                    new XElement("numeroFactureSaisi", fields[5]),
                    new XElement("dateFacture", fields[6]),
                    new XElement("deviseFacture", fields[7]),
                    new XElement("typeFacture", fields[8]),
                    new XElement("typeTva", fields[9]),
                    new XElement("numeroMarche", fields[10]),
                    new XElement("numeroBonCommande", fields[11]),
                    new XElement("numeroFactureOrigine", fields[12]),
                    new XElement("modePaiement", fields[13]),
                    new XElement("lignePoste",
                        new XElement("lignePosteNumero", fields[14]),
                        new XElement("lignePosteReference", fields[15]),
                        new XElement("lignePosteDenomination", fields[16]),
                        new XElement("lignePosteQunatite", fields[17]),
                        new XElement("lignePosteUnite", fields[18]),
                        new XElement("lignePosteMontantUnitaireHT", fields[19]),
                        new XElement("lignePosteMontantRemiseHT", fields[20]),
                        new XElement("lignePosteTauxTva", fields[21]),
                        new XElement("ligneTvaTaux", fields[22]),
                        new XElement("ligneTvaMontantBaseHtParTaux", fields[23]),
                        new XElement("ligneTvaMontantTvaParTaux", fields[24])
                        ),
                    new XElement("montantHtTotal", fields[25]),
                    new XElement("montantTVA", fields[26]),
                    new XElement("montantTtcTotal", fields[27]),
                    new XElement("montantRemiseGlobalTTC", fields[28]),
                    new XElement("montantAPayer", fields[29]),
                    new XElement("commentaire", fields[30])
                )
            );
Puis j'arrive à lire des informations ciblées par ce moyen:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
//je récupère la conversion xml sous forme de texte car j'utilise par la suite WCF qui ne serialize pas les XDocument, mais le problème n'est pas là
            var fichier = fac.ToString();
 
            using (XmlReader reader = XmlReader.Create(new StringReader(fichier)))
            {
                while (reader.Read())
                {
                    //un exemple d' "information ciblée"
                    reader.Name.Equals("numeroFactureSaisi");                    
                }
            }
Mon problème est le suivant, le fichier csv est fait d'une telle façon qu'à chaque nouvelle ligne de commande appartenant à une même facture, cette nouvelle ligne de commande se retrouve dans la prochaine facture/ligne(du fichier csv), au "niveau" suivant dans le fichier xml.
J'aimerais reconstruire mon xml de façon à ce que chaque facture ai toutes ses lignes de commandes à la suite.

Voici un extrait du fichier xml converti d'une facture avec deux lignes de commandes afin d’illustrer mes propos:
(Je précise que les deux factures ci-dessous n'en sont en fait qu'une seule.)
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
<Root>
  <Facture>
    <Site>49</Site>
    <codeDestinataire>2625017600****</codeDestinataire>
    <codeServiceExecutant>SERVICE</codeServiceExecutant>
    <idFournisseur>54 208 370 *** ***</idFournisseur>
    <codeCadreFacturation>A1_FACTURE_FOURNISSEUR</codeCadreFacturation>
    <numeroFactureSaisi>49500****</numeroFactureSaisi>
    <dateFacture>03/03/2014</dateFacture>
    <deviseFacture>EUR</deviseFacture>
    <typeFacture>FACTURE</typeFacture>
    <typeTva>TVA_SUR_DEBIT</typeTva>
    <numeroMarche>250562117****</numeroMarche>
    <numeroBonCommande>BON N�1</numeroBonCommande>
    <numeroFactureOrigine>0</numeroFactureOrigine>
    <modePaiement>VIREMENT</modePaiement>
    <lignePoste>
      <lignePosteNumero>1</lignePosteNumero>
      <lignePosteReference>BABICOC</lignePosteReference>
      <lignePosteDenomination>MATELAS POSITIONNEMENT BABICOC</lignePosteDenomination>
      <lignePosteQunatite>1</lignePosteQunatite>
      <lignePosteUnite>Pi�ce</lignePosteUnite>
      <lignePosteMontantUnitaireHT>1507,11</lignePosteMontantUnitaireHT>
      <lignePosteMontantRemiseHT>0</lignePosteMontantRemiseHT>
      <lignePosteTauxTva>1</lignePosteTauxTva>
      <ligneTvaTaux>1</ligneTvaTaux>
      <ligneTvaMontantBaseHtParTaux>1507,11</ligneTvaMontantBaseHtParTaux>
      <ligneTvaMontantTvaParTaux>82,89</ligneTvaMontantTvaParTaux>
    </lignePoste>
    <montantHtTotal>1529,69</montantHtTotal>
    <montantTVA>87,32</montantTVA>
    <montantTtcTotal>1617,01</montantTtcTotal>
    <montantRemiseGlobalTTC>0</montantRemiseGlobalTTC>
    <montantAPayer>1617,01</montantAPayer>
    <commentaire></commentaire>
  </Facture>
  <Facture>
    <Site>49</Site>
    <codeDestinataire>2625017600****</codeDestinataire>
    <codeServiceExecutant>SERVICE</codeServiceExecutant>
    <idFournisseur>54 208 370 *** ***</idFournisseur>
    <codeCadreFacturation>A1_FACTURE_FOURNISSEUR</codeCadreFacturation>
    <numeroFactureSaisi>49500****</numeroFactureSaisi>
    <dateFacture>03/03/2014</dateFacture>
    <deviseFacture>EUR</deviseFacture>
    <typeFacture>FACTURE</typeFacture>
    <typeTva>TVA_SUR_DEBIT</typeTva>
    <numeroMarche>250562117****</numeroMarche>
    <numeroBonCommande>BON N�2</numeroBonCommande>
    <numeroFactureOrigine>0</numeroFactureOrigine>
    <modePaiement>VIREMENT</modePaiement>
    <lignePoste>
      <lignePosteNumero>2</lignePosteNumero>
      <lignePosteReference>FRAISGAO</lignePosteReference>
      <lignePosteDenomination>FRAIS D'EXPEDITION</lignePosteDenomination>
      <lignePosteQunatite>1</lignePosteQunatite>
      <lignePosteUnite>Pi�ce</lignePosteUnite>
      <lignePosteMontantUnitaireHT>22,58</lignePosteMontantUnitaireHT>
      <lignePosteMontantRemiseHT>0</lignePosteMontantRemiseHT>
      <lignePosteTauxTva>2</lignePosteTauxTva>
      <ligneTvaTaux>2</ligneTvaTaux>
      <ligneTvaMontantBaseHtParTaux>22,58</ligneTvaMontantBaseHtParTaux>
      <ligneTvaMontantTvaParTaux>4,43</ligneTvaMontantTvaParTaux>
    </lignePoste>
    <montantHtTotal>1529,69</montantHtTotal>
    <montantTVA>87,32</montantTVA>
    <montantTtcTotal>1617,01</montantTtcTotal>
    <montantRemiseGlobalTTC>0</montantRemiseGlobalTTC>
    <montantAPayer>1617,01</montantAPayer>
    <commentaire></commentaire>
  </Facture>
Dites moi si vous ne m'avez pas compris j'essaierai d'être plus précis.

Merci d'avance si réponse il y a.