Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

DataGrid et XML: charger les documents XML dans un DataGrid aussi enregistrer les données d’un DataGrid dans un document XML

Utilisation,de,la,classe,'XmlTextReader',pour,lire,un,document,XML
{filelink=16387}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
 
public class DataGridXML : System.Windows.Forms.Form
{
   private System.Windows.Forms.Button btnChargDtGrid;
    private System.Windows.Forms.Button btnEngDtGrid;
   private System.Windows.Forms.DataGrid MonDataGrid;
   private System.ComponentModel.Container components = null;
 
   public DataGridXML() {
      InitializeComponent();
   }
 
   private void InitializeComponent() {
       this.btnChargDtGrid = new System.Windows.Forms.Button();
       this.btnEngDtGrid = new System.Windows.Forms.Button();
       this.MonDataGrid = new System.Windows.Forms.DataGrid();
       ((System.ComponentModel.ISupportInitialize)(this.MonDataGrid)).BeginInit();
       this.SuspendLayout();
       //
       // btnChargDtGrid
       //
       this.btnChargDtGrid.Location = new System.Drawing.Point(67, 206);
       this.btnChargDtGrid.Name = "btnChargDtGrid";
       this.btnChargDtGrid.Size = new System.Drawing.Size(168, 23);
       this.btnChargDtGrid.TabIndex = 0;
       this.btnChargDtGrid.Text = "Charger le Document XML";
       this.btnChargDtGrid.Click += new System.EventHandler(this.btnReadXml_Click);
       //
       // btnEngDtGrid
       //
       this.btnEngDtGrid.Location = new System.Drawing.Point(241, 206);
       this.btnEngDtGrid.Name = "btnEngDtGrid";
       this.btnEngDtGrid.Size = new System.Drawing.Size(177, 23);
       this.btnEngDtGrid.TabIndex = 1;
       this.btnEngDtGrid.Text = "Enregistrer le DataGrid En XML";
       this.btnEngDtGrid.Click += new System.EventHandler(this.btnWriteXml_Click);
       //
       // MonDataGrid
       //
       this.MonDataGrid.DataMember = "";
       this.MonDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
       this.MonDataGrid.Location = new System.Drawing.Point(1, 0);
       this.MonDataGrid.Name = "MonDataGrid";
       this.MonDataGrid.Size = new System.Drawing.Size(581, 200);
       this.MonDataGrid.TabIndex = 3;
       //
       // DataGridXML
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(579, 272);
       this.Controls.Add(this.MonDataGrid);
       this.Controls.Add(this.btnEngDtGrid);
       this.Controls.Add(this.btnChargDtGrid);
       this.Name = "DataGridXML";
       this.Text = "Liaison DataGrid Document XML";
       ((System.ComponentModel.ISupportInitialize)(this.MonDataGrid)).EndInit();
       this.ResumeLayout(false);
 
   }
 
   static void Main() {
      Application.Run(new DataGridXML());
   }
 
   private void btnReadXml_Click(object sender, System.EventArgs e) {
      DataSet ds = new DataSet();
      ds.ReadXml("Operations.xml",XmlReadMode.InferSchema);
      MonDataGrid.SetDataBinding(ds, "Clients");
   }
 
   private void btnWriteXml_Click(object sender, System.EventArgs e) {
      DataSet ds = (DataSet) MonDataGrid.DataSource;
      ds.WriteXml("DataGrid.xml",XmlWriteMode.IgnoreSchema);
   }
 
}
 
/*
 
//Fichier XML: Operation.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="MesExemples.xsl"?>
<MesOperations>
    <Clients>
        <IDClient>CCLiege</IDClient>
        <Entreprise>Genital</Entreprise>
        <Nom-Contact>Frederic Moulard</Nom-Contact>
        <Fonction-Contact>D.G Commercial</Fonction-Contact>
        <Adresse>15 Rue X</Adresse>
        <Ville>Paris</Ville>
        <CodePostal>2025</CodePostal>
        <Pays>France</Pays>
        <Tel>20-30-14-30-65</Tel>
        <Fax>20-30-14-30-66</Fax>
    </Clients>
    <Clients>
        <IDClient>CCVerge</IDClient>
        <Entreprise>XMey</Entreprise>
        <Nom-Contact>Herve Banste</Nom-Contact>
        <Fonction-Contact>PDG</Fonction-Contact>
        <Adresse>30, rue des Galliers</Adresse>
        <Ville>Marseille</Ville>
        <CodePostal>13008</CodePostal>
        <Pays>France</Pays>
        <Tel>32-15-22-16-63</Tel>
        <Fax>32-15-22-16-64</Fax>
    </Clients>
    <Clients>
        <IDClient>CCDerge</IDClient>
        <Entreprise>Fourgon</Entreprise>
        <Nom-Contact>Samuel Etienne</Nom-Contact>
        <Fonction-Contact>D.G Commercial</Fonction-Contact>
        <Adresse>Cener Str. 213</Adresse>
        <Ville>Berlin</Ville>
        <CodePostal>12209</CodePostal>
        <Pays>Allemagne</Pays>
        <Tel>044-2014587</Tel>
        <Fax>044-2014588</Fax>
    </Clients>
</MesOperations>
 
*/

No comments yet.

Leave a Comment