IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Extraire des informations d'un fichier xml deserialisation


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut Extraire des informations d'un fichier xml deserialisation
    Bonjour,
    Possible suite de cette discussion https://www.developpez.net/forums/d1...d-fichier-xml/
    Voici mon fichier XML (généré par un concepteur du framework, bref)
    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    <Form Name="Form1" Type="System.Windows.Forms.Form, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Left="15" Top="15" Width="174" Height="300" Text="" BackColorR="240">
      <Button Name="Button2" Type="System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Left="19" Top="12" Width="114" Height="57" Text="Button2" BackColorR="0" />
      <TextBox Name="TextBox2" Type="System.Windows.Forms.TextBox, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Left="19" Top="203" Width="100" Height="20" Text="" BackColorR="255" />
      <TextBox Name="TextBox1" Type="System.Windows.Forms.TextBox, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Left="19" Top="177" Width="100" Height="20" Text="" BackColorR="255" />
      <Button Name="Button1" Type="System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Left="19" Top="135" Width="120" Height="62" Text="Button1" BackColorR="240" />
    </Form>
    Moi je veux extraire les infos du genre la nature de l'objet (button, textbox...) le nom (Name="le nom") la position (Left = "" Width = "" etc...) et etc..
    Comme cela je peux créer mon petit wrapper maison pour que la ligne lue dans le fichier xml
    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    <Form Name="Form1" Type="System.Windows.Forms.Form, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Left="15" Top="15" Width="174" Height="300" Text="hello" BackColorR="240">
    se transforme dans un autre fichier en parallèle en
    window/ Form1
    .px = "15"
    .py = "15"
    .tx = "174"
    .ty = "300"
    .titre = "hello"
    create/
    End/ window
    Ce qui donne pour ces 5 objets xml le fichier suivant :

    window/ Form1
    .px = "15"
    .py = "15"
    .tx = "174"
    .ty = "300"
    .titre = "hello"
    create/
    End/ window

    button/ button2
    .px = "19"
    .py = "12"
    .tx = "114"
    .ty = "57"
    .titre = "button2"
    create/
    End/ button

    Etc.....
    Etc....
    Etc......
    J'espère que vous pourrez m'aider, je compte sur vous.
    Bonne soirée

  2. #2
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    Bonjour,
    Nom : Sans titre.png
Affichages : 578
Taille : 6,2 Ko
    Voici un exemple lire ton fichier xml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    Imports System.Xml
     
    Public Class Form1
        Dim FilePath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\simulator.xml"
     
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Try
                ListView1.Items.Clear()
                Dim doc As XmlDocument = New XmlDocument()
                doc.Load(FilePath)
                Dim nodes As XmlNode = doc.SelectSingleNode("/Form")
                Dim name0 As String = String.Empty, Left1 As String = String.Empty, Top2 As String = String.Empty
                Dim Width3 As String = String.Empty, Height4 As String = String.Empty, Text5 As String = String.Empty
                Dim Backcolor6 As String = String.Empty
                For i = 0 To nodes.ChildNodes.Count - 1
                    name0 = nodes.ChildNodes(i).Attributes.GetNamedItem("Name").Value
                    Left1 = nodes.ChildNodes(i).Attributes.GetNamedItem("Left").Value
                    Top2 = nodes.ChildNodes(i).Attributes.GetNamedItem("Top").Value
                    Width3 = nodes.ChildNodes(i).Attributes.GetNamedItem("Width").Value
                    Height4 = nodes.ChildNodes(i).Attributes.GetNamedItem("Height").Value
                    Text5 = nodes.ChildNodes(i).Attributes.GetNamedItem("Text").Value
                    Backcolor6 = nodes.ChildNodes(i).Attributes.GetNamedItem("BackColorR").Value
                    ListView1.Items.Add(New ListViewItem(New String() {name0, Left1, Top2, Width3, Height4, Text5, Backcolor6}))
                Next
            Catch ex As Exception
                MessageBox.Show("Erreur de lecture", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    End Class
    cordialement,

  3. #3
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Bonjour,
    merci c'est fonctionnel après avoir déclarer la variable i as integer.
    Cependant, comment faire pour générer mon code , disons à chaque nœud xml correspond à un bloc de code et on ajoute tous ces blocs dans une richtextbox par exemple.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    window/ Form1
    .px = "15"
    .py = "15"
    .tx = "174"
    .ty = "300"
    .titre = "hello"
    create/
    End/ window
     
    button/ button2
    .px = "19"
    .py = "12"
    .tx = "114"
    .ty = "57"
    .titre = "button2"
    create/
    End/ button
     
    Etc.....
    Etc....
    Etc......
    J'ai rajouté cette variable pour savoir si il s'agit d'un bouton ou autres

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Type = nodes.ChildNodes(i).Name
    Je vous remercie

  4. #4
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    Bonjour,
    Nom : xml.PNG
Affichages : 548
Taille : 3,4 Ko

    voici un exemple que vous-pouvez faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    Imports System.Xml
     
    Public Class Form1
        Dim FilePath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\simulator.xml"
     
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Try
                RichTextBox1.Clear()
                Dim doc As XmlDocument = New XmlDocument()
                doc.Load(FilePath)
                Dim nodes As XmlNodeList = doc.SelectNodes("/Form")
                Dim name0 As String = String.Empty, Left1 As String = String.Empty, Top2 As String = String.Empty
                Dim Width3 As String = String.Empty, Height4 As String = String.Empty, Text5 As String = String.Empty
                Dim Backcolor6 As String = String.Empty
                For i = 0 To nodes.Count - 1
                    name0 = nodes(i).Attributes.GetNamedItem("Name").Value
                    Left1 = nodes(i).Attributes.GetNamedItem("Left").Value
                    Top2 = nodes(i).Attributes.GetNamedItem("Top").Value
                    Width3 = nodes(i).Attributes.GetNamedItem("Width").Value
                    Height4 = nodes(i).Attributes.GetNamedItem("Height").Value
                    Text5 = nodes(i).Attributes.GetNamedItem("Text").Value
                    Backcolor6 = nodes(i).Attributes.GetNamedItem("BackColorR").Value
                Next
                Dim textxml As String = "Windows/ " & name0 & ChrW(10) & _
                    ".px= " & """" & Left1 & """" & ChrW(10) & _
                    ".py = " & """" & Top2 & """" & ChrW(10) & _
                    ".tx = " & """" & Width3 & """" & ChrW(10) & _
                    ".ty = " & """" & Height4 & """" & ChrW(10) & _
                    ".titre = " & """" & Text5 & """" & ChrW(10) & _
                    "create/" & ChrW(10) & _
                    "End/ window"
                RichTextBox1.AppendText(textxml)
            Catch ex As Exception
                MessageBox.Show("Erreur de lecture", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    End Class
    Cordialement,

  5. #5
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Bonjour,
    merci effectivement c'est ce que j'avais essayé mais le problème est que ça génère que un objet, et pas tous les objets du fichier xml les uns en dessous des autres dans la Richtextbox (qui sont d'ailleurs dans la listview du précédent exemple).
    comment peut on faire
    J'avoue que je sèche
    Merci

  6. #6
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    salut,

    tous les objets du fichier xml les uns en dessous des autres dans la Richtextbox (qui sont d'ailleurs dans la listview du précédent exemple).
    tu peut faire comme cela pour tous afficher dans richtextbox1 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    Imports System.Xml
     
    Public Class Form1
        Dim FilePath As String = Application.StartupPath & "\simulator.xml"
     
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Try
                RichTextBox1.Clear()
                Dim doc As XmlDocument = New XmlDocument()
                doc.Load(FilePath)
                Dim nodes As XmlNode = doc.SelectSingleNode("/Form")
                Dim name0 As String = String.Empty, Left1 As String = String.Empty, Top2 As String = String.Empty
                Dim Width3 As String = String.Empty, Height4 As String = String.Empty, Text5 As String = String.Empty
                Dim Backcolor6 As String = String.Empty, title As String = String.Empty
                For i = 0 To nodes.ChildNodes.Count - 1
                    title = nodes.ChildNodes(i).Name
                    name0 = nodes.ChildNodes(i).Attributes.GetNamedItem("Name").Value
                    Left1 = nodes.ChildNodes(i).Attributes.GetNamedItem("Left").Value
                    Top2 = nodes.ChildNodes(i).Attributes.GetNamedItem("Top").Value
                    Width3 = nodes.ChildNodes(i).Attributes.GetNamedItem("Width").Value
                    Height4 = nodes.ChildNodes(i).Attributes.GetNamedItem("Height").Value
                    Text5 = nodes.ChildNodes(i).Attributes.GetNamedItem("Text").Value
                    Backcolor6 = nodes.ChildNodes(i).Attributes.GetNamedItem("BackColorR").Value
                    Dim textxml As String = title & "/ " & name0 & ChrW(10) & _
                    ".px= " & """" & Left1 & """" & ChrW(10) & _
                    ".py = " & """" & Top2 & """" & ChrW(10) & _
                    ".tx = " & """" & Width3 & """" & ChrW(10) & _
                    ".ty = " & """" & Height4 & """" & ChrW(10) & _
                    ".titre = " & """" & Text5 & """" & ChrW(10) & _
                    "create/" & ChrW(10) & _
                    "End/ " & title & ChrW(10) & Chr(10)
                    RichTextBox1.AppendText(textxml)
                Next
            Catch ex As Exception
                MessageBox.Show("Erreur de lecture", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
     
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If RichTextBox1.TextLength > 0 Then
                Me.RichTextBox1.SaveFile(My.Computer.FileSystem.SpecialDirectories.Desktop & "\MyXml.xml", RichTextBoxStreamType.PlainText) 'sauvegarder tous ce qui ce trouve dans richtextbox en XML
                MsgBox("Fichier Enregistrer!")
            End If
        End Sub
     
    End Class
    dans button1, je t'ai ajouter comment enregistrer avec richtextbox1...
    cordialement,

Discussions similaires

  1. Réponses: 0
    Dernier message: 15/04/2009, 15h57
  2. comment extraire des donnees sur un fichier xml en java
    Par mgueye dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 15/05/2008, 14h31
  3. extraire les informations d'un fichier XML
    Par touf35 dans le forum C++Builder
    Réponses: 5
    Dernier message: 29/02/2008, 18h16
  4. Extraire des données d'un fichier XML
    Par ultimate_manx dans le forum XML
    Réponses: 1
    Dernier message: 28/11/2007, 18h03
  5. Réponses: 2
    Dernier message: 20/10/2006, 10h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo