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
SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|