par , 18/11/2016 à 20h45 (701 Affichages)
La serialization xml :
1 classe Evaluation :
1 2 3 4 5 6 7 8
|
<Serializable>
Public Class Evaluation
Public Version As String
Public EtudeRendement As EtudeRendement
End Class |
1 classe EtudeRendement :
1 2 3 4 5 6 7 8 9
|
<Serializable>
Public Class EtudeRendement
Public DateDeb As String
Public NbTx As String
Public Apr As String
End Class |
1 classe de gestion AppSettings :
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
|
Imports System.Xml.Serialization
Imports System.IO
<Serializable>
Public Class AppSettings
#Region " Fields "
Public Evaluation As Evaluation
<XmlIgnore()>
Public XmlPath As String = My.Application.Info.DirectoryPath & "\Conf.xml"
#End Region
#Region " Methods "
''' <summary>
''' Enregistre l'état courant de la classe dans un fichier au format XML.
''' </summary>
Public Sub SaveFile()
Dim serializer As New XmlSerializer(GetType(AppSettings))
Dim ecrivain As New StreamWriter(XmlPath)
serializer.Serialize(ecrivain, Me)
ecrivain.Close()
End Sub
''' <summary>
''' Charge l'état courant du fichier XML.
''' </summary>
''' <returns>Valeur de type AppSettings</returns>
Public Shared Function LoadFile() As AppSettings
Dim deserializer As New XmlSerializer(GetType(AppSettings))
Dim lecteur As New StreamReader(My.Application.Info.DirectoryPath & "\Conf.xml")
Dim p As AppSettings = DirectCast(deserializer.Deserialize(lecteur), AppSettings)
lecteur.Close()
Return p
End Function
#End Region
End Class |
1 projet test d'enregistrement avec 3 textbox et 1 bouton :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
Public Class Form1
Private m_Settings As AppSettings
Sub New()
InitializeComponent()
m_Settings = New AppSettings
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim etud As New EtudeRendement With {.DateDeb = TextBox1.Text,
.NbTx = TextBox2.Text,
.Apr = TextBox3.Text}
Dim eval As New Evaluation With {.EtudeRendement = etud,
.Version = "3.1"}
m_Settings.Evaluation = eval
m_Settings.SaveFile()
End Sub
End Class |
Le contenu du fichier .xml après l'enregistrement :
1 2 3 4 5 6 7 8 9 10 11 12
|
<?xml version="1.0" encoding="utf-8"?>
<AppSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Evaluation>
<Version>3.1</Version>
<EtudeRendement>
<DateDeb>01/12/2016</DateDeb>
<NbTx>Trvx</NbTx>
<Apr>0</Apr>
</EtudeRendement>
</Evaluation>
</AppSettings> |
Un langage objet pour de la programmation orienté objet