Accueil > > > EXEMPLE DE SÉRIALISATION D'OBJETS
EXEMPLE DE SÉRIALISATION D'OBJETS
Information sur la source
Description
Rien de bien compliqué, juste histoire de montrer comment sérialiser un objet de votre application.
Source
- Imports System.Xml
- Imports System.Xml.Serialization
- Imports System.IO
- Imports System.Text
-
-
-
- Public Class Humain
- Public Nom As String
- Public Prenom As String
- Public DateNaissance As String
- End Class
-
- Module Serial
-
- Dim Fichier As String = "Objet_Humain.xml"
- Dim Objet_Humain As Humain
-
- Public Sub Deserialiser()
- Try
- Dim x As XmlSerializer = New XmlSerializer(GetType(Humain))
- Dim fs As New FileStream(Fichier, FileMode.Open)
- Dim r As New XmlTextReader(fs)
-
- Objet_Humain = CType(x.Deserialize(r), Humain)
- r.Close()
- fs.Close()
-
- Catch ex As Exception
- MsgBox(ex.ToString)
- End Try
- End Sub
-
-
- Public Sub Serialiser()
- Try
- Dim x As XmlSerializer = New XmlSerializer(GetType(Humain))
-
- Dim fs As New FileStream(Fichier, FileMode.Create)
- Dim w As New XmlTextWriter(fs, New UTF8Encoding)
- w.Formatting = Formatting.Indented
- x.Serialize(w, Objet_Humain)
- w.Close()
- fs.Close()
-
- Catch ex As Exception
- MsgBox(ex.ToString)
- End Try
- End Sub
-
-
- Public Sub main()
- Try
- Objet_Humain = New Humain
- Objet_Humain.Nom = "X"
- Objet_Humain.Prenom = "Senynium"
- Objet_Humain.DateNaissance = "07/08/80"
-
- MessageBox.Show("Nom : " & Objet_Humain.Nom & vbCrLf & "Prenom : " & Objet_Humain.Prenom & vbCrLf & "Ne le : " & Objet_Humain.DateNaissance, "Exemple de sérialisation", MessageBoxButtons.OK)
-
- Serialiser()
-
- Objet_Humain = New Humain
- Objet_Humain.Nom = "A changé"
- Objet_Humain.Prenom = "Achangé"
- Objet_Humain.DateNaissance = "A changé"
-
- MessageBox.Show("Nom : " & Objet_Humain.Nom & vbCrLf & "Prenom : " & Objet_Humain.Prenom & vbCrLf & "Ne le : " & Objet_Humain.DateNaissance, "Exemple de sérialisation", MessageBoxButtons.OK)
-
- Objet_Humain = New Humain
- Deserialiser()
-
-
- MessageBox.Show("Nom : " & Objet_Humain.Nom & vbCrLf & "Prenom : " & Objet_Humain.Prenom & vbCrLf & "Ne le : " & Objet_Humain.DateNaissance, "Exemple de sérialisation", MessageBoxButtons.OK)
-
-
- Catch ex As Exception
- MsgBox(ex.ToString)
- End Try
- End Sub
-
- End Module
-
-
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports System.Text
Public Class Humain
Public Nom As String
Public Prenom As String
Public DateNaissance As String
End Class
Module Serial
Dim Fichier As String = "Objet_Humain.xml"
Dim Objet_Humain As Humain
Public Sub Deserialiser()
Try
Dim x As XmlSerializer = New XmlSerializer(GetType(Humain))
Dim fs As New FileStream(Fichier, FileMode.Open)
Dim r As New XmlTextReader(fs)
Objet_Humain = CType(x.Deserialize(r), Humain)
r.Close()
fs.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub Serialiser()
Try
Dim x As XmlSerializer = New XmlSerializer(GetType(Humain))
Dim fs As New FileStream(Fichier, FileMode.Create)
Dim w As New XmlTextWriter(fs, New UTF8Encoding)
w.Formatting = Formatting.Indented
x.Serialize(w, Objet_Humain)
w.Close()
fs.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub main()
Try
Objet_Humain = New Humain
Objet_Humain.Nom = "X"
Objet_Humain.Prenom = "Senynium"
Objet_Humain.DateNaissance = "07/08/80"
MessageBox.Show("Nom : " & Objet_Humain.Nom & vbCrLf & "Prenom : " & Objet_Humain.Prenom & vbCrLf & "Ne le : " & Objet_Humain.DateNaissance, "Exemple de sérialisation", MessageBoxButtons.OK)
Serialiser()
Objet_Humain = New Humain
Objet_Humain.Nom = "A changé"
Objet_Humain.Prenom = "Achangé"
Objet_Humain.DateNaissance = "A changé"
MessageBox.Show("Nom : " & Objet_Humain.Nom & vbCrLf & "Prenom : " & Objet_Humain.Prenom & vbCrLf & "Ne le : " & Objet_Humain.DateNaissance, "Exemple de sérialisation", MessageBoxButtons.OK)
Objet_Humain = New Humain
Deserialiser()
MessageBox.Show("Nom : " & Objet_Humain.Nom & vbCrLf & "Prenom : " & Objet_Humain.Prenom & vbCrLf & "Ne le : " & Objet_Humain.DateNaissance, "Exemple de sérialisation", MessageBoxButtons.OK)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Module
Conclusion
C'est juste à titre d'exemple donc ... L'objet est sérialisé et écrit dans un fichier xml. Utile pour les échanges entre applications, ou pour un transfert internet.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Sérialisation d'un objet [ par Ougalougou ]
Je voudrais un exemple simple de création d'un objet 'personne" avec comme attributs "nom" et "prénom" et la sérialisation dans un fichier pour les co
sérialisation d'une application [ par progrima ]
bonjour tout le monde! je cherche à sécuriser des applications qui existent déjà et pour cela j'ai pensé à génerer des numéros de série, j'explique: 1
sérialisation [ par monvbfrance ]
salut tout le mondeje voudrais un module qui fait la transformation d'un fichier xml ou xsd aux classes exploitable par le DOTNET. est ce que vous pou
Sérialisation, génération de classes via XSD.exe [ par bacchusmaster ]
Bonjour à tous,Je suis tombé sur un article (http://c2i.fr/code.aspx?IDCode=606) qui traite exactement de ce que je veux faire mai
[DEBUTANT] sérialisation binaire de plusieurs objets [ par xawax34 ]
Bonjour,Je voudrais faire une sérialisation binaire de plusieurs objets dans un seul fichier et lorsque je désérialise, chacun devra se retrouver dans
sérialisation et sockets [ par Kite37 ]
Bonjour ! Alors voici ma question : Pour le besoin de mon logiciel, je me retrouve dans l'obligation d'envoyer des objets complet via socket.Ceux-ci n
Désérialisation en visual studio 2005 [ par Grefer2luxe ]
Bonjour,Je fais actuellement une application en visual studio 2005 et je rencontre un souci. En effet, j'ai réussi à sérialiser dans mon programme mai
problème sérialisation [ par jabaka ]
Bonjour je vous expose mon problème que voici:J'ai trouver quelques sources qui parlais de sérialisation et même quelque un qui traitait du RichTextBo
Sérialisation d'une form (au moment du design) [ par veler ]
Salut,J'ai crée un concepteur de fenêtre, j'aimerais pouvoir obtenir une class (un bout de code quoi) à partir du contenu du concepteur (design surfac
Problème avec la désérialisation binaire [ par veler ]
Salut,J'ai un piti problème avec la désérialisation :J'ai le code suivant :Dim myFileStream As IO.Stream = IO.File.OpenRead(tmp_NomCompletFichier)Dim
|
Derniers Blogs
PAS D'INTELLITRACE SUR MON SITE WEB DANS IIS !PAS D'INTELLITRACE SUR MON SITE WEB DANS IIS ! par Etienne Margraff
J'ai récemment eu un problème pour obtenir l'intelliTrace sur un site web dans IIS. Il n'y avait pas de message d'erreur, rien dans le journal d'évènement Windows, et après 3 appels à une voyante, 2 visites chez un marabou, j'ai failli me résign...
Cliquez pour lire la suite de l'article par Etienne Margraff OFFICE 365 - SHAREPOINT ONLINE, QUELQUES LIMITATIONSOFFICE 365 - SHAREPOINT ONLINE, QUELQUES LIMITATIONS par junarnoalg
De nombreuses entreprises font le choix de SharePoint Online, service fourni au travers de l'offre de Microsoft Office 365. S'il est vrai que ce choix apporte un grand nombre d'avantages; rapidité de mise en œuvre, disponibilité, large couvertu...
Cliquez pour lire la suite de l'article par junarnoalg PRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGEPRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGE par richardc
http://www.c2idotnet.com/articles/presentation-des-api-rest-de-windows-azure-lister-les-comptes-de-storage
Désolé pour "toto", mais c2i existait avant blogs.developpeur.org et c'est mon site "officiel" ;-) ...
Cliquez pour lire la suite de l'article par richardc [HTML5] SLIDES ET DéMOS : AUTOUR DU W3C , NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] SLIDES ET DéMOS : AUTOUR DU W3C , NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Très bonne après-midi passée lors cette conférence avec le W3C, organisée par L' Inria sur les nouveaux standards, ce Mardi 14 Février, on sent vraiment que çà bosse au W3C, et l'avenir est très très prometteur pour le HTML5, notamment ...
Cliquez pour lire la suite de l'article par Gio
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate 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
|