Exemple d'utilisation du xml avec vb.net

Description

C'est une source très simple qui permettra au débutants de faire leur marques avec le XML dans VB.NET, ce code n'a pas la prétention d'être exhaustif mais il permet au moins de se faire une idée des propriétés et des méthodes disponibles.
Pour le reste tout est dans le zip...

ATTENTION
- Le format du zip est en 7-Zip
- L'application est en VB.NET 2010

Source / Exemple :


Imports System.Xml
Imports System.IO
Imports System.Xml.XPath

Public Class fMain
    Dim sSampleXMLFile As String = Application.StartupPath & "\SampleXMLFile.xml"
    Dim sSampleQueryFile As String = Application.StartupPath & "\QueryOnXML.xml"

    ' Très bon tutorial sur XPAth http://jerome.developpez.com/xmlxsl/xpath/?lpage=exemples
    ' Exemple LINQ => http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx

    Private Sub btCreateXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCreateXML.Click
        ' This method shows how to build an XML file all from code.
        Dim xDoc As New XmlDocument

        Dim xPI As XmlProcessingInstruction
        Dim xComment As XmlComment
        Dim xElmntRoot As XmlElement
        Dim xElmntContent As XmlElement

        xPI = xDoc.CreateProcessingInstruction("xml", "version='1.0'")
        xDoc.AppendChild(xPI)

        ' Add a comment in start of document
        xComment = xDoc.CreateComment("Sample of a comment in a XML file")
        xDoc.AppendChild(xComment)

        xElmntRoot = xDoc.CreateElement("xml")
        xDoc.AppendChild(xElmntRoot)

        ' Create a root and a sub
        xElmntContent = xElmntRoot.AppendChild(xDoc.CreateElement("CHILD_1"))
        xElmntContent.AppendChild(xDoc.CreateElement("SUB_CHILD_1"))

        ' Create a root and a sub 2
        xElmntContent = xElmntRoot.AppendChild(xDoc.CreateElement("SECTION_2"))
        xElmntContent.AppendChild(xDoc.CreateElement("SUB_2"))

        ' Save document
        xDoc.Save(sSampleXMLFile)

        'Show doc
        btShowXMLDocument_Click(sender, e)
    End Sub

    Private Sub btShowXMLDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btShowXMLDocument.Click
        'Shell("notepad " & sSampleXMLFile, AppWinStyle.NormalFocus)
        rtbXMLDocument.Clear()
        rtbXMLDocument.LoadFile(sSampleXMLFile, RichTextBoxStreamType.PlainText)
    End Sub

    Private Sub btAddElements_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAddElements.Click
        Dim xDoc As New XmlDocument
        Dim xNode As XmlNode
        Dim xNodeTemp As XmlNode

        'Load XML document
        xDoc.Load(sSampleXMLFile)

        ' Select for a particular node
        xNode = xDoc.SelectSingleNode("//SUB_CHILD_1")
        ' Add a TAG content with text in SECTION_1_SUB_1 section
        xNodeTemp = xDoc.CreateElement("ELEMENT")
        xNodeTemp.AppendChild(xDoc.CreateTextNode("Sample of a text in a node"))
        xNode.AppendChild(xNodeTemp)

        ' Save file
        xDoc.Save(sSampleXMLFile)

        'Show doc
        btShowXMLDocument_Click(sender, e)
    End Sub

    Private Sub btAddAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAddAttributes.Click
        ' This method will remove all child nodes of the Family 
        ' node and then re-add them with some attributes.
        ' It also shows how to manipulate existing attributes.
        Dim xDoc As New XmlDocument
        Dim xElem As XmlElement

        xDoc.Load(sSampleXMLFile)

        ' Select ELEMENT_1 and add one attribute
        xElem = xDoc.SelectSingleNode("//SUB_CHILD_1/ELEMENT")
        xElem.SetAttribute("Attribute_1", "Value of attribute 1")
        xElem.SetAttribute("Attribute_2", "Value of attribute the big attribute 2")

        ' Save file
        xDoc.Save(sSampleXMLFile)
        'Show doc
        btShowXMLDocument_Click(sender, e)
    End Sub

    Private Sub btModifyElements_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btModifyElements.Click
        Dim xDoc As New XmlDocument
        Dim xElem As XmlElement

        xDoc.Load(sSampleXMLFile)

        ' Select ELEMENT_1 and add one attribute
        xElem = xDoc.SelectSingleNode("//SUB_CHILD_1/ELEMENT")
        xElem.InnerText = "The new texte of the element"

        ' Save file
        xDoc.Save(sSampleXMLFile)
        'Show doc
        btShowXMLDocument_Click(sender, e)
    End Sub

    Private Sub btModifyAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btModifyAttributes.Click
        Dim xDoc As New XmlDocument
        Dim xElem As XmlElement

        xDoc.Load(sSampleXMLFile)

        ' Select ELEMENT_1 and add one attribute
        xElem = xDoc.SelectSingleNode("//SUB_CHILD_1/ELEMENT")
        xElem.SetAttribute("Attribute_1", "The new value of attribute 1")

        ' Save file
        xDoc.Save(sSampleXMLFile)
        'Show doc
        btShowXMLDocument_Click(sender, e)
    End Sub

    Private Sub btDeleteAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDeleteAttributes.Click
        Dim xDoc As New XmlDocument
        Dim xElem As XmlElement

        xDoc.Load(sSampleXMLFile)

        ' Select ELEMENT_1 and add one attribute
        xElem = xDoc.SelectSingleNode("//SUB_CHILD_1/ELEMENT")
        xElem.RemoveAttribute("Attribute_1")

        ' Save file
        xDoc.Save(sSampleXMLFile)
        'Show doc
        btShowXMLDocument_Click(sender, e)
    End Sub

    Private Sub btDeleteElements_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDeleteElements.Click
        Dim xDoc As New XmlDocument
        Dim xElem As XmlElement

        xDoc.Load(sSampleXMLFile)

        ' Select ELEMENT_1 and add one attribute
        xElem = xDoc.SelectSingleNode("//SUB_CHILD_1/ELEMENT")
        xElem.RemoveAll()

        ' Save file
        xDoc.Save(sSampleXMLFile)
        'Show doc
        btShowXMLDocument_Click(sender, e)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRecursiveItems.Click
        Dim xDoc As New XmlDocument
        Dim sWriter As New StringWriter
        Dim sArrayListOfNodes As New ArrayList

        xDoc.Load(sSampleXMLFile)

        ' Use a recursive function to visit all nodes.
        ParseTree(sArrayListOfNodes, xDoc, 0)

        rtbXMLDocument.Clear()
        For Each sNode In sArrayListOfNodes
            rtbXMLDocument.Text += sNode.ToString & vbCrLf
        Next
    End Sub

    Private Sub ParseTree(ByVal aArrNodes As ArrayList, ByVal xNode As XmlNode, ByVal iLevel As Integer)
        ' current node and convert it with spaces
        Dim s As New String(System.Convert.ToChar(vbTab), iLevel)
        ' Add node to collection
        aArrNodes.Add(s & xNode.NodeType.ToString & " Level : " & iLevel & " => Node name : " & xNode.Name.ToString)

        ' If the current node has children also run recursivity
        If xNode.HasChildNodes Then
            For Each xNodeLoop In xNode.ChildNodes
                ParseTree(aArrNodes, xNodeLoop, iLevel + 1)
            Next xNodeLoop
        End If
    End Sub

    Private Sub btGetElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetElement.Click
        Dim xpathDoc As XPathDocument
        Dim xmlNav As XPathNavigator
        Dim xmlNI As XPathNodeIterator

        xpathDoc = New XPathDocument(sSampleXMLFile)
        xmlNav = xpathDoc.CreateNavigator()

        xmlNI = xmlNav.Select("/xml/CHILD_1")

        rtbXMLDocument.Clear()
        While (xmlNI.MoveNext())
            rtbXMLDocument.Text += xmlNI.Current.Name & " : " & xmlNI.Current.Value
        End While
    End Sub

    Private Sub btGetAttribut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetAttribut.Click
        Dim xDoc As New XmlDocument
        Dim xElem As XmlElement
        Dim xAttr As XmlAttribute

        xDoc.Load(sSampleXMLFile)

        ' Select ELEMENT_1 and add one attribute
        xElem = xDoc.SelectSingleNode("/xml/CHILD_1/SUB_CHILD_1/ELEMENT")

        rtbXMLDocument.Clear()
        For Each xAttr In xElem.Attributes
            rtbXMLDocument.Text += xAttr.Name & " : " & xAttr.Value & vbCrLf
        Next
    End Sub

    Private Sub fMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btElementHasAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btElementHasAttributes.Click
        Dim xDoc As New XmlDocument
        Dim xElem As XmlElement

        xDoc.Load(sSampleXMLFile)

        ' Select ELEMENT_1 and add one attribute
        xElem = xDoc.SelectSingleNode("/xml/CHILD_1/SUB_CHILD_1/ELEMENT")
        rtbXMLDocument.Text += xElem.Name & " : " & xElem.HasAttribute("Attribute_1") & vbCrLf
    End Sub
End Class

Conclusion :


Il n'y a pas vraiment de conclusion à vous de vous faire une idée...

Codes Sources

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.