Partage
  • Partager sur Facebook
  • Partager sur Twitter

Insérer les données d'un xml dans un dataGridview

Anonyme
    4 mai 2019 à 3:48:48

    Bonjour, je souhaiterais insérer des données en provenance d'un fichier xml

    serveurs.xml

    <serveurs>
    <serveur numero="1" name="[FR] floflo530 & nenelpsm titan only" map="suez_canal" joueur="24/48" pass="0" ip="192.168.1.1" port="17567" type="rcl"/>
    <serveur numero="2" name="[FR] nenelpsm titan only" map="suez_canal2" joueur="21/48" pass="0" ip="192.168.1.2" port="17568" type="nov"/>
    </serveurs>

    J'arrive à les afficher de manière static avec

                string[,] rows = new string[,]
                {
                    {"1", "[FR] floflo530 & nenelpsm titan only", "Suez_Canal", "24/48", "non" },
                    {"2", "[FR] nenelpsm titan only", "Suez_Canal2", "23/48", "oui" }
                };

    Mais impossible en faisant un foreach

    voici le morceau de code en question :

    private void Button7_Click(object sender, EventArgs e)
            {
                dataGridView1.Rows.Clear();
                XmlDocument monFichiers = new XmlDocument();
                monFichiers.Load("serveurs.xml");
    
                XmlNodeList lstt = monFichiers.GetElementsByTagName("serveur");
    
                foreach (XmlNode nn in lstt)
                {
                    // nn.Attributes[0].Value, nn.Attributes[1].Value, nn.Attributes[2].Value, nn.Attributes[3].Value, nn.Attributes[4].Value 
                }
                string[,] rows = new string[,]
                {
                    {"1", "[FR] floflo530 & nenelpsm titan only", "Suez_Canal", "24/48", "non" },
                    {"2", "[FR] nenelpsm titan only", "Suez_Canal2", "23/48", "oui" }
                };
    
                for(int i =0; i < rows.GetLength(0); i++)
                {
                    string[] row = new string[rows.GetLongLength(1)];
    
                    for(int j = 0; j < rows.GetLength(1); j++)
                    {
                        row[j] = rows[i, j];
                    }
                    dataGridView1.Rows.Add(row);
                }
            }



    • Partager sur Facebook
    • Partager sur Twitter
      9 mai 2019 à 16:43:12

      Faire d'un DataSet la DataSource de votre DataGridView.

      Pour charger le DataSet :

      https://docs.microsoft.com/fr-fr/dotnet/framework/data/adonet/dataset-datatable-dataview/loading-a-dataset-from-xml

      • Partager sur Facebook
      • Partager sur Twitter
      Je recherche un CDI/CDD/mission freelance comme Architecte Logiciel/ Expert Technique sur technologies Microsoft.

      Insérer les données d'un xml dans un dataGridview

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown