Accueil > > > APPLICATION POUR LIRE, FAIRE UNE AJOUT DANS UN FICHIER XML
APPLICATION POUR LIRE, FAIRE UNE AJOUT DANS UN FICHIER XML
Information sur la source
Description
ce code simple permet d'avoir de bonne base pour lire et ajouter des éléments d'un fichier XML.
Source
- 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
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
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[RIA SERVICES] INCLUDE ET DOMAINDATASOURCE[RIA SERVICES] INCLUDE ET DOMAINDATASOURCE par Audrey
Dans un de mes articles précédents , j'avais parlé des DomainDataSource avec RIA Services dans le cas d'une interface Maître - Détail. Dans le même principe, je vais parler d'une autre manière de mettre en forme ce cas d'interface avec RIA Services. Et po...
Cliquez pour lire la suite de l'article par Audrey ZUNE : VERSION ZUNE SOFTWARE V 4.2 ET LA SOCIALISATIONZUNE : VERSION ZUNE SOFTWARE V 4.2 ET LA SOCIALISATION par ROMELARD Fabrice
Une des nouveautés de la version V 3.0 était l'apparition de l'onglet Social qui ne fonctionnait que si le MarketPlace était activé sur son poste. Cela limitait donc son intérêt, car hors du cadre commercial USA-CANADA, peu de monde trouva...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice PRATIQUE DE SILVERLIGHT PAR ERIC AMBROSIPRATIQUE DE SILVERLIGHT PAR ERIC AMBROSI par MPOWARE
Je viens de finir la lecture du dernier livre d'
Eric Ambrosi
éditions PEARSON
Son livre donne une approche pratique de Silverlight qui sera aussi bien comprise par le développeur que par le designeur.
Tous les aspects du développement RIA sont abor...
Cliquez pour lire la suite de l'article par MPOWARE APPRENDRE à DéVELOPPER POUR LES MOBILES AVEC LA NOUVELLE GéNéRATION .NETAPPRENDRE à DéVELOPPER POUR LES MOBILES AVEC LA NOUVELLE GéNéRATION .NET par odewit
2 déclinaisons de Silverlight et 2 déclinaisons de Mono permettent dorénavant (ou permettront prochainement) de développer des applications .NET mobiles pour les principales plates-formes du marché :
Silverlight pour Symbian, basé sur Silverlight 2...
Cliquez pour lire la suite de l'article par odewit ZUNE : NOUVELLE VERSION DU ZUNE SOFTWARE - V 4.2ZUNE : NOUVELLE VERSION DU ZUNE SOFTWARE - V 4.2 par ROMELARD Fabrice
Avec la dernière génération du lecteur MP3 de Microsoft, le ZUNE HD, Microsoft a publié une nouvelle version du logiciel pour PC. Ainsi, je me suis décidé à installer celle-ci sur mon Tablet PC ACER, comme toujours le logiciel est donc tél...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (10.9.4.0)ACADEMY SYSTEM (10.9.4.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods
|