none
WPF Ajouter des données à une base de donnée Access depuis un fichier xml RRS feed

  • Question

  • Bonjour, voici un code qui récupère le contenu d'un ficher xml pour en insérer le contenu dans une base de donnée Access.

    Ce code me renvois une erreur "Null Value" alors que dans le fichier xml j'ai bien des données.

    Voici le fichier xml :

    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
      <STOCKS_CLIENTS>
        <STK_CODE>6253</STK_CODE>
        <ART_CODE>100CLI36</ART_CODE>
        <STK_DENT>2019-01-17T00:00:00+01:00</STK_DENT>
        <STK_CLI_NOM>TROPAL</STK_CLI_NOM>
        <STK_NUMPAL>191171112</STK_NUMPAL>
        <STK_PICKING>104.A3</STK_PICKING>
        <ART_LIBELLE1>FOIE BF CARTON</ART_LIBELLE1>
        <STK_PROD>TROPAL</STK_PROD>
        <STK_FOUR>FR 01.053.024 CE</STK_FOUR>
        <STK_PBRUT>336</STK_PBRUT>
        <STK_TPAL>22</STK_TPAL>
        <STK_TCART>0</STK_TCART>
        <STK_PNET>314</STK_PNET>
        <STK_LOT1>190150183</STK_LOT1>
        <STK_LOT2>-</STK_LOT2>
        <STK_LOT3>-</STK_LOT3>
        <STK_LOT4>-</STK_LOT4>
        <STK_LOT5>-</STK_LOT5>
        <STK_LOT6>-</STK_LOT6>
        <STK_NBU>1</STK_NBU>
        <STK_DATECONG>2019-01-14T00:00:00+01:00</STK_DATECONG>
        <STK_DLUO>2020-07-14T00:00:00+02:00</STK_DLUO>
        <ART_SIGNEQ>O</ART_SIGNEQ>
      </STOCKS_CLIENTS>
      <STOCKS_CLIENTS>
        <STK_CODE>6254</STK_CODE>
        <ART_CODE>100CLI36</ART_CODE>
        <STK_DENT>2019-01-17T00:00:00+01:00</STK_DENT>
        <STK_CLI_NOM>TROPAL</STK_CLI_NOM>
        <STK_NUMPAL>191171145</STK_NUMPAL>
        <STK_PICKING>104.A3</STK_PICKING>
        <ART_LIBELLE1>FOIE BF CARTON</ART_LIBELLE1>
        <STK_PROD>TROPAL</STK_PROD>
        <STK_FOUR>FR 01.053.024 CE</STK_FOUR>
        <STK_PBRUT>362</STK_PBRUT>
        <STK_TPAL>15</STK_TPAL>
        <STK_TCART>0</STK_TCART>
        <STK_PNET>347</STK_PNET>
        <STK_LOT1>190150188</STK_LOT1>
        <STK_LOT2>-</STK_LOT2>
        <STK_LOT3>-</STK_LOT3>
        <STK_LOT4>-</STK_LOT4>
        <STK_LOT5>-</STK_LOT5>
        <STK_LOT6>-</STK_LOT6>
        <STK_NBU>1</STK_NBU>
        <STK_DATECONG>2019-01-10T00:00:00+01:00</STK_DATECONG>
        <STK_DLUO>2020-07-10T00:00:00+02:00</STK_DLUO>
        <ART_SIGNEQ>O</ART_SIGNEQ>
      </STOCKS_CLIENTS>
      <STOCKS_CLIENTS>
        <STK_CODE>6255</STK_CODE>
        <ART_CODE>100CLI36</ART_CODE>
        <STK_DENT>2019-01-17T00:00:00+01:00</STK_DENT>
        <STK_CLI_NOM>TROPAL</STK_CLI_NOM>
        <STK_NUMPAL>191171222</STK_NUMPAL>
        <STK_PICKING>104.B3</STK_PICKING>
        <ART_LIBELLE1>FOIE BF CARTON</ART_LIBELLE1>
        <STK_PROD>TROPAL</STK_PROD>
        <STK_FOUR>FR 01.053.024 CE</STK_FOUR>
        <STK_PBRUT>337</STK_PBRUT>
        <STK_TPAL>22</STK_TPAL>
        <STK_TCART>0</STK_TCART>
        <STK_PNET>315</STK_PNET>
        <STK_LOT1>190110251</STK_LOT1>
        <STK_LOT2>-</STK_LOT2>
        <STK_LOT3>-</STK_LOT3>
        <STK_LOT4>-</STK_LOT4>
        <STK_LOT5>-</STK_LOT5>
        <STK_LOT6>-</STK_LOT6>
        <STK_NBU>1</STK_NBU>
        <STK_DATECONG>2019-01-09T00:00:00+01:00</STK_DATECONG>
        <STK_DLUO>2020-07-09T00:00:00+02:00</STK_DLUO>
        <ART_SIGNEQ>O</ART_SIGNEQ>
      </STOCKS_CLIENTS>
      <STOCKS_CLIENTS>
        <STK_CODE>6256</STK_CODE>
        <ART_CODE>100CLI36</ART_CODE>
        <STK_DENT>2019-01-17T00:00:00+01:00</STK_DENT>
        <STK_CLI_NOM>TROPAL</STK_CLI_NOM>
        <STK_NUMPAL>191171253</STK_NUMPAL>
        <STK_PICKING>104.B3</STK_PICKING>
        <ART_LIBELLE1>FOIE BF CARTON</ART_LIBELLE1>
        <STK_PROD>TROPAL</STK_PROD>
        <STK_FOUR>FR 01.053.024 CE</STK_FOUR>
        <STK_PBRUT>344</STK_PBRUT>
        <STK_TPAL>23</STK_TPAL>
        <STK_TCART>0</STK_TCART>
        <STK_PNET>321</STK_PNET>
        <STK_LOT1>190110252</STK_LOT1>
        <STK_LOT2>-</STK_LOT2>
        <STK_LOT3>-</STK_LOT3>
        <STK_LOT4>-</STK_LOT4>
        <STK_LOT5>-</STK_LOT5>
        <STK_LOT6>-</STK_LOT6>
        <STK_NBU>1</STK_NBU>
        <STK_DATECONG>2019-01-08T00:00:00+01:00</STK_DATECONG>
        <STK_DLUO>2020-07-08T00:00:00+02:00</STK_DLUO>
        <ART_SIGNEQ>O</ART_SIGNEQ>
      </STOCKS_CLIENTS>
      <STOCKS_CLIENTS>
        <STK_CODE>6257</STK_CODE>
        <ART_CODE>100CLI36</ART_CODE>
        <STK_DENT>2019-01-17T00:00:00+01:00</STK_DENT>
        <STK_CLI_NOM>TROPAL</STK_CLI_NOM>
        <STK_NUMPAL>191171321</STK_NUMPAL>
        <STK_PICKING>FDCH</STK_PICKING>
        <ART_LIBELLE1>FOIE BF CARTON</ART_LIBELLE1>
        <STK_PROD>TROPAL</STK_PROD>
        <STK_FOUR>FR 01.053.024 CE</STK_FOUR>
        <STK_PBRUT>372</STK_PBRUT>
        <STK_TPAL>32</STK_TPAL>
        <STK_TCART>0</STK_TCART>
        <STK_PNET>340</STK_PNET>
        <STK_LOT1>190100313</STK_LOT1>
        <STK_LOT2>-</STK_LOT2>
        <STK_LOT3>-</STK_LOT3>
        <STK_LOT4>-</STK_LOT4>
        <STK_LOT5>-</STK_LOT5>
        <STK_LOT6>-</STK_LOT6>
        <STK_NBU>1</STK_NBU>
        <STK_DATECONG>2019-01-08T00:00:00+01:00</STK_DATECONG>
        <STK_DLUO>2020-07-08T00:00:00+02:00</STK_DLUO>
        <ART_SIGNEQ>O</ART_SIGNEQ>
      </STOCKS_CLIENTS>
      <STOCKS_CLIENTS>
        <STK_CODE>6258</STK_CODE>
        <ART_CODE>100CLI36</ART_CODE>
        <STK_DENT>2019-01-17T00:00:00+01:00</STK_DENT>
        <STK_CLI_NOM>TROPAL</STK_CLI_NOM>
        <STK_NUMPAL>191171352</STK_NUMPAL>
        <STK_PICKING>068.B2</STK_PICKING>
        <ART_LIBELLE1>FOIE BF CARTON</ART_LIBELLE1>
        <STK_PROD>TROPAL</STK_PROD>
        <STK_FOUR>FR 01.053.024 CE</STK_FOUR>
        <STK_PBRUT>350</STK_PBRUT>
        <STK_TPAL>28</STK_TPAL>
        <STK_TCART>0</STK_TCART>
        <STK_PNET>322</STK_PNET>
        <STK_LOT1>190090296</STK_LOT1>
        <STK_LOT2>-</STK_LOT2>
        <STK_LOT3>-</STK_LOT3>
        <STK_LOT4>-</STK_LOT4>
        <STK_LOT5>-</STK_LOT5>
        <STK_LOT6>-</STK_LOT6>
        <STK_NBU>1</STK_NBU>
        <STK_DATECONG>2019-01-07T00:00:00+01:00</STK_DATECONG>
        <STK_DLUO>2020-07-07T00:00:00+02:00</STK_DLUO>
        <ART_SIGNEQ>O</ART_SIGNEQ>
      </STOCKS_CLIENTS>
    </DocumentElement>

    Et le code C# :

      string xmlPath = $@"{ApplicationDirectories.dossierDoc}\{ApplicationDirectories.appName}\Dossiers {ApplicationDirectories.appName}\Xml_Files";
                    string xmlName = $@"{xmlPath}\SoldePalettesCli.xml";
                    string codeStock,artcode;
                    XmlDocument xml = new XmlDocument();
                    xml.Load(xmlName);
                   
                    foreach (XmlNode row in xml.ChildNodes)
                    {
                        #region Insert Into Commandes temp
                        codeStock = row.SelectSingleNode("STK_CODE").InnerText;
                        artcode = row.SelectSingleNode("ART_CODE").InnerText;
    
                        comm = new OleDbCommand("INSERT INTO CLIENTS_COMMANDE_TEMP (STK_CODE,ART_CODE) VALUES ('" + codeStock + "','" + artcode + "')",conn);
                        conn.Open();
                        comm.ExecuteNonQuery();
                        conn.Close();
                        #endregion
                    }

    Et le message d'erreur :


    JF Collombet ® CreateSpecificCulture

    dimanche 27 janvier 2019 17:10

Toutes les réponses

  • bonjour Jean-Francois,

    apparemment la variable row ne pointe pas sur la bonne ligne, pouvez vous essayez ce changement au niveau du code : 

    foreach (XmlNode row in xml.ChildNodes[1].ChildNodes)
    {
       #region Insert Into Commandes temp
       // l'erreur est du au fait que le parser ne trouve pas le node "STK_CODE" 
       // vu qu'on pointe sur le premier child, il faut pointer sur les childNodes du deuxieme child 
       codeStock = row.SelectSingleNode("STK_CODE")?.InnerText ?? "Null";
       artcode = row.SelectSingleNode("ART_CODE")?.InnerText ?? "Null";
       // ..... le reste du code 
    }

    Cordialement,

    Mouad.

    samedi 2 février 2019 12:32