Application pour lire, faire une ajout dans un fichier xml

Contenu du snippet

ce code simple permet d'avoir de bonne base pour lire et ajouter des éléments d'un fichier XML.

Source / Exemple :


Imports System.Xml

Namespace AppXML

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private documentXML As XmlDocument
        Private noeuxRacine As XmlNode
        Private EnfantNoeux As XmlNodeList

        Private elementXML As XmlElement
        Private texteXML As XmlText

        Private sTousLesEnfants As String
        Private Const CHEMIN As String = "C:\Formules.xml"
        Private sNouvelleForm As String

#Region " Code généré par le Concepteur Windows Form "

        Public Sub New()
            MyBase.New()

            'Cet appel est requis par le Concepteur Windows Form.
            InitializeComponent()

            'Ajoutez une initialisation quelconque après l'appel InitializeComponent()

        End Sub

        'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Requis par le Concepteur Windows Form
        Private components As System.ComponentModel.IContainer

        'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form
        'Elle peut être modifiée en utilisant le Concepteur Windows Form.  
        'Ne la modifiez pas en utilisant l'éditeur de code.
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents TxtXMLAjout As System.Windows.Forms.TextBox
        Friend WithEvents BtnLire As System.Windows.Forms.Button
        Friend WithEvents BtnAjout As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
            Me.BtnLire = New System.Windows.Forms.Button
            Me.BtnAjout = New System.Windows.Forms.Button
            Me.TxtXMLAjout = New System.Windows.Forms.TextBox
            Me.Label1 = New System.Windows.Forms.Label
            Me.SuspendLayout()
            '
            'BtnLire
            '
            Me.BtnLire.Location = New System.Drawing.Point(32, 72)
            Me.BtnLire.Name = "BtnLire"
            Me.BtnLire.Size = New System.Drawing.Size(112, 32)
            Me.BtnLire.TabIndex = 1
            Me.BtnLire.Text = "LireXML"
            '
            'BtnAjout
            '
            Me.BtnAjout.Location = New System.Drawing.Point(152, 72)
            Me.BtnAjout.Name = "BtnAjout"
            Me.BtnAjout.Size = New System.Drawing.Size(112, 32)
            Me.BtnAjout.TabIndex = 2
            Me.BtnAjout.Text = "AJoutXML"
            '
            'TxtXMLAjout
            '
            Me.TxtXMLAjout.Location = New System.Drawing.Point(48, 32)
            Me.TxtXMLAjout.Name = "TxtXMLAjout"
            Me.TxtXMLAjout.Size = New System.Drawing.Size(176, 20)
            Me.TxtXMLAjout.TabIndex = 3
            Me.TxtXMLAjout.Text = ""
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(48, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(184, 24)
            Me.Label1.TabIndex = 4
            Me.Label1.Text = "Formule de politesse"
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 125)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.TxtXMLAjout)
            Me.Controls.Add(Me.BtnAjout)
            Me.Controls.Add(Me.BtnLire)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.Name = "Form1"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.Text = "Gestionnaire XML"
            Me.ResumeLayout(False)

        End Sub

#End Region

        Private Sub BtnLire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLire.Click

            Dim iIndexe As Integer

            Try
                documentXML = New XmlDocument

                documentXML.Load(CHEMIN)

                noeuxRacine = documentXML.DocumentElement()

                EnfantNoeux = noeuxRacine.ChildNodes

                iIndexe = 0

                While (iIndexe < EnfantNoeux.Count)

                    MsgBox(EnfantNoeux.ItemOf(iIndexe).InnerText)
                    iIndexe = iIndexe + 1

                End While

            Catch ex As XmlException

                MessageBox.Show(ex.Message, "Problème XML", MessageBoxButtons.OK, MessageBoxIcon.Error)

            Catch ex As Exception

                MessageBox.Show(ex.Message, "Problème", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

        End Sub

        Private Sub BtnAjout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAjout.Click

            Try

                sNouvelleForm = TxtXMLAjout.Text

                documentXML = New XmlDocument

                documentXML.Load(CHEMIN)

                EnfantNoeux = documentXML.GetElementsByTagName("Intro")

                Dim i As Integer

                For i = 0 To EnfantNoeux.Count - 1

                    If EnfantNoeux.Item(i).InnerText = sNouvelleForm Then
                        MessageBox.Show("Element déjà ajouté", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    End If

                Next

                elementXML = documentXML.CreateElement("Intro")
                texteXML = documentXML.CreateTextNode(sNouvelleForm)

                documentXML.DocumentElement.AppendChild(elementXML)
                documentXML.DocumentElement.LastChild.AppendChild(texteXML)

                documentXML.Save(CHEMIN)

                MessageBox.Show("Ajout effectué", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Catch ex As XmlException

                MessageBox.Show(ex.Message, "Problème XML", MessageBoxButtons.OK, MessageBoxIcon.Error)

            Catch ex As Exception

                MessageBox.Show(ex.Message, "Problème", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try
        End Sub

    End Class

End Namespace

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.