How to Deserialize in VB.NET











up vote
0
down vote

favorite












I am new to serialization/deserialization in VB.net. I have generated this class with xsd2code:



Namespace Classe_FtpTypes

#Region "Base entity class"
Partial Public Class EntityBase(Of T)
Implements System.ComponentModel.INotifyPropertyChanged

Private Shared sSerializer As System.Xml.Serialization.XmlSerializer

Private Shared ReadOnly Property Serializer() As System.Xml.Serialization.XmlSerializer
Get
If (sSerializer Is Nothing) Then
sSerializer = New System.Xml.Serialization.XmlSerializer(GetType(T))
End If
Return sSerializer
End Get
End Property

Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Overridable Sub OnPropertyChanged(ByVal propertyName As String)
Dim handler As System.ComponentModel.PropertyChangedEventHandler = Me.PropertyChangedEvent
If (Not (handler) Is Nothing) Then
handler(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
End If
End Sub

#Region "Serialize/Deserialize"
'''<summary>
'''Serializes current EntityBase object into an XML document
'''</summary>
'''<returns>string XML value</returns>
Public Overloads Overridable Function SerializzaXml(ByVal encoding As System.Text.Encoding) As String
Dim streamReader As System.IO.StreamReader = Nothing
Dim memoryStream As System.IO.MemoryStream = Nothing
Try
memoryStream = New System.IO.MemoryStream()
Dim xmlWriterSettings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
xmlWriterSettings.Encoding = encoding
xmlWriterSettings.Indent = True
Dim xmlWriter As System.Xml.XmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings)
Serializer.Serialize(xmlWriter, Me)
memoryStream.Seek(0, System.IO.SeekOrigin.Begin)
streamReader = New System.IO.StreamReader(memoryStream)
Return streamReader.ReadToEnd
Finally
If (Not (streamReader) Is Nothing) Then
streamReader.Dispose
End If
If (Not (memoryStream) Is Nothing) Then
memoryStream.Dispose
End If
End Try
End Function

Public Overloads Overridable Function SerializzaXml() As String
Return SerializzaXml(Encoding.UTF8)
End Function

'''<summary>
'''Deserializes workflow markup into an EntityBase object
'''</summary>
'''<param name="xml">string workflow markup to deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = DeserializzaXml(xml)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return DeserializzaXml(xml, obj, exception)
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String) As T
Dim stringReader As System.IO.StringReader = Nothing
Try
stringReader = New System.IO.StringReader(xml)
Return CType(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)),T)
Finally
If (Not (stringReader) Is Nothing) Then
stringReader.Dispose
End If
End Try
End Function

'''<summary>
'''Serializes current EntityBase object into file
'''</summary>
'''<param name="fileName">full path of outupt xml file</param>
'''<param name="exception">output Exception value if failed</param>
'''<returns>true if can serialize and save into file; otherwise, false</returns>
Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef exception As System.Exception) As Boolean
exception = Nothing
Try
SalvaXml(fileName, encoding)
Return true
Catch e As System.Exception
exception = e
Return false
End Try
End Function

Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByRef exception As System.Exception) As Boolean
Return SalvaXml(fileName, Encoding.UTF8, exception)
End Function

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String)
SalvaXml(fileName, Encoding.UTF8)
End Sub

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding)
Dim streamWriter As System.IO.StreamWriter = Nothing
Try
Dim xmlString As String = SerializzaXml(encoding)
streamWriter = New System.IO.StreamWriter(fileName, false, Encoding.UTF8)
streamWriter.WriteLine(xmlString)
streamWriter.Close
Finally
If (Not (streamWriter) Is Nothing) Then
streamWriter.Dispose
End If
End Try
End Sub

'''<summary>
'''Deserializes xml markup from file into an EntityBase object
'''</summary>
'''<param name="fileName">string xml file to load and deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = CaricaXml(fileName, encoding)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
Return CaricaXml(fileName, Encoding.UTF8, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return CaricaXml(fileName, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String) As T
Return CaricaXml(fileName, Encoding.UTF8)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding) As T
Dim file As System.IO.FileStream = Nothing
Dim sr As System.IO.StreamReader = Nothing
Try
file = New System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read)
sr = New System.IO.StreamReader(file, encoding)
Dim xmlString As String = sr.ReadToEnd
sr.Close
file.Close
Return DeserializzaXml(xmlString)
Finally
If (Not (file) Is Nothing) Then
file.Dispose
End If
If (Not (sr) Is Nothing) Then
sr.Dispose
End If
End Try
End Function
#End Region
End Class
#End Region

Partial Public Class QuadraturaFTP_Type
Inherits EntityBase(Of QuadraturaFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraCreazioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroFileField As List(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.numeroFileField = New List(Of TipoFile_Type)()
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraCreazione() As Date
Get
Return Me.dataOraCreazioneField
End Get
Set
If (dataOraCreazioneField.Equals(value) <> true) Then
Me.dataOraCreazioneField = value
Me.OnPropertyChanged("DataOraCreazione")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

<System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
System.Xml.Serialization.XmlArrayItemAttribute("File", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=false)> _
Public Property NumeroFile() As List(Of TipoFile_Type)
Get
Return Me.numeroFileField
End Get
Set
If (Not (Me.numeroFileField) Is Nothing) Then
If (numeroFileField.Equals(value) <> true) Then
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
Else
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Partial Public Class TipoFile_Type
Inherits EntityBase(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private tipoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroField As String

Public Property Tipo() As String
Get
Return Me.tipoField
End Get
Set
If (Not (Me.tipoField) Is Nothing) Then
If (tipoField.Equals(value) <> true) Then
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
Else
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
End Set
End Property

Public Property Numero() As String
Get
Return Me.numeroField
End Get
Set
If (Not (Me.numeroField) Is Nothing) Then
If (numeroField.Equals(value) <> true) Then
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
Else
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
End Set
End Property
End Class

Partial Public Class EsitoFTP_Type
Inherits EntityBase(Of EsitoFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraRicezioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraEsitoField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private esitoField As EsitoTrasferimentoFTP_Type

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraRicezione() As Date
Get
Return Me.dataOraRicezioneField
End Get
Set
If (dataOraRicezioneField.Equals(value) <> true) Then
Me.dataOraRicezioneField = value
Me.OnPropertyChanged("DataOraRicezione")
End If
End Set
End Property

Public Property DataOraEsito() As Date
Get
Return Me.dataOraEsitoField
End Get
Set
If (dataOraEsitoField.Equals(value) <> true) Then
Me.dataOraEsitoField = value
Me.OnPropertyChanged("DataOraEsito")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

Public Property Esito() As EsitoTrasferimentoFTP_Type
Get
Return Me.esitoField
End Get
Set
If (esitoField.Equals(value) <> true) Then
Me.esitoField = value
Me.OnPropertyChanged("Esito")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Public Enum EsitoTrasferimentoFTP_Type

'''<remarks/>
ET01

'''<remarks/>
ET02
End Enum

Partial Public Class NumeroFile_Type
Inherits EntityBase(Of NumeroFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private fileField As List(Of TipoFile_Type)

'''<summary>
'''NumeroFile_Type class constructor
'''</summary>
Public Sub New()
MyBase.New
Me.fileField = New List(Of TipoFile_Type)()
End Sub

Public Property File() As List(Of TipoFile_Type)
Get
Return Me.fileField
End Get
Set
If (Not (Me.fileField) Is Nothing) Then
If (fileField.Equals(value) <> true) Then
Me.fileField = value
Me.OnPropertyChanged("File")
End If
Else
Me.fileField = value
Me.OnPropertyChanged("File")
End If
End Set
End Property
End Class
End Namespace


No problem with the serialization, but I can not use the deserialization. I tried to use it with:



Dim fileEsito As New Classe_FtpTypes.EsitoFTP_Type


but fileEsito gives me only SerializzaXml and SalvaXml. No chance to use DeserializzaXml and CaricaXml.



Thanks in advance for any help.










share|improve this question
























  • Because DeserializzaXml is a Shared function and as such must be called on Classe_FtpTypes.EsitoFTP_Type, not on fileEsito?
    – GSerg
    Nov 12 at 17:24












  • Your serialize function and de-serialize function must use the same 'T' for the code to work. From what I see in the code, "T" is not the same.
    – jdweng
    Nov 12 at 19:28










  • Thank you, I was able to use CaricaXml using: Dim fileEsito As Classe_FtpTypes.EsitoFTP_Type = Classe_FtpTypes.EsitoFTP_Type.CaricaXml(file_destinazione)
    – Jonathan Quid
    Nov 14 at 14:49

















up vote
0
down vote

favorite












I am new to serialization/deserialization in VB.net. I have generated this class with xsd2code:



Namespace Classe_FtpTypes

#Region "Base entity class"
Partial Public Class EntityBase(Of T)
Implements System.ComponentModel.INotifyPropertyChanged

Private Shared sSerializer As System.Xml.Serialization.XmlSerializer

Private Shared ReadOnly Property Serializer() As System.Xml.Serialization.XmlSerializer
Get
If (sSerializer Is Nothing) Then
sSerializer = New System.Xml.Serialization.XmlSerializer(GetType(T))
End If
Return sSerializer
End Get
End Property

Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Overridable Sub OnPropertyChanged(ByVal propertyName As String)
Dim handler As System.ComponentModel.PropertyChangedEventHandler = Me.PropertyChangedEvent
If (Not (handler) Is Nothing) Then
handler(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
End If
End Sub

#Region "Serialize/Deserialize"
'''<summary>
'''Serializes current EntityBase object into an XML document
'''</summary>
'''<returns>string XML value</returns>
Public Overloads Overridable Function SerializzaXml(ByVal encoding As System.Text.Encoding) As String
Dim streamReader As System.IO.StreamReader = Nothing
Dim memoryStream As System.IO.MemoryStream = Nothing
Try
memoryStream = New System.IO.MemoryStream()
Dim xmlWriterSettings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
xmlWriterSettings.Encoding = encoding
xmlWriterSettings.Indent = True
Dim xmlWriter As System.Xml.XmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings)
Serializer.Serialize(xmlWriter, Me)
memoryStream.Seek(0, System.IO.SeekOrigin.Begin)
streamReader = New System.IO.StreamReader(memoryStream)
Return streamReader.ReadToEnd
Finally
If (Not (streamReader) Is Nothing) Then
streamReader.Dispose
End If
If (Not (memoryStream) Is Nothing) Then
memoryStream.Dispose
End If
End Try
End Function

Public Overloads Overridable Function SerializzaXml() As String
Return SerializzaXml(Encoding.UTF8)
End Function

'''<summary>
'''Deserializes workflow markup into an EntityBase object
'''</summary>
'''<param name="xml">string workflow markup to deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = DeserializzaXml(xml)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return DeserializzaXml(xml, obj, exception)
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String) As T
Dim stringReader As System.IO.StringReader = Nothing
Try
stringReader = New System.IO.StringReader(xml)
Return CType(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)),T)
Finally
If (Not (stringReader) Is Nothing) Then
stringReader.Dispose
End If
End Try
End Function

'''<summary>
'''Serializes current EntityBase object into file
'''</summary>
'''<param name="fileName">full path of outupt xml file</param>
'''<param name="exception">output Exception value if failed</param>
'''<returns>true if can serialize and save into file; otherwise, false</returns>
Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef exception As System.Exception) As Boolean
exception = Nothing
Try
SalvaXml(fileName, encoding)
Return true
Catch e As System.Exception
exception = e
Return false
End Try
End Function

Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByRef exception As System.Exception) As Boolean
Return SalvaXml(fileName, Encoding.UTF8, exception)
End Function

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String)
SalvaXml(fileName, Encoding.UTF8)
End Sub

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding)
Dim streamWriter As System.IO.StreamWriter = Nothing
Try
Dim xmlString As String = SerializzaXml(encoding)
streamWriter = New System.IO.StreamWriter(fileName, false, Encoding.UTF8)
streamWriter.WriteLine(xmlString)
streamWriter.Close
Finally
If (Not (streamWriter) Is Nothing) Then
streamWriter.Dispose
End If
End Try
End Sub

'''<summary>
'''Deserializes xml markup from file into an EntityBase object
'''</summary>
'''<param name="fileName">string xml file to load and deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = CaricaXml(fileName, encoding)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
Return CaricaXml(fileName, Encoding.UTF8, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return CaricaXml(fileName, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String) As T
Return CaricaXml(fileName, Encoding.UTF8)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding) As T
Dim file As System.IO.FileStream = Nothing
Dim sr As System.IO.StreamReader = Nothing
Try
file = New System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read)
sr = New System.IO.StreamReader(file, encoding)
Dim xmlString As String = sr.ReadToEnd
sr.Close
file.Close
Return DeserializzaXml(xmlString)
Finally
If (Not (file) Is Nothing) Then
file.Dispose
End If
If (Not (sr) Is Nothing) Then
sr.Dispose
End If
End Try
End Function
#End Region
End Class
#End Region

Partial Public Class QuadraturaFTP_Type
Inherits EntityBase(Of QuadraturaFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraCreazioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroFileField As List(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.numeroFileField = New List(Of TipoFile_Type)()
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraCreazione() As Date
Get
Return Me.dataOraCreazioneField
End Get
Set
If (dataOraCreazioneField.Equals(value) <> true) Then
Me.dataOraCreazioneField = value
Me.OnPropertyChanged("DataOraCreazione")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

<System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
System.Xml.Serialization.XmlArrayItemAttribute("File", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=false)> _
Public Property NumeroFile() As List(Of TipoFile_Type)
Get
Return Me.numeroFileField
End Get
Set
If (Not (Me.numeroFileField) Is Nothing) Then
If (numeroFileField.Equals(value) <> true) Then
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
Else
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Partial Public Class TipoFile_Type
Inherits EntityBase(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private tipoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroField As String

Public Property Tipo() As String
Get
Return Me.tipoField
End Get
Set
If (Not (Me.tipoField) Is Nothing) Then
If (tipoField.Equals(value) <> true) Then
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
Else
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
End Set
End Property

Public Property Numero() As String
Get
Return Me.numeroField
End Get
Set
If (Not (Me.numeroField) Is Nothing) Then
If (numeroField.Equals(value) <> true) Then
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
Else
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
End Set
End Property
End Class

Partial Public Class EsitoFTP_Type
Inherits EntityBase(Of EsitoFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraRicezioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraEsitoField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private esitoField As EsitoTrasferimentoFTP_Type

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraRicezione() As Date
Get
Return Me.dataOraRicezioneField
End Get
Set
If (dataOraRicezioneField.Equals(value) <> true) Then
Me.dataOraRicezioneField = value
Me.OnPropertyChanged("DataOraRicezione")
End If
End Set
End Property

Public Property DataOraEsito() As Date
Get
Return Me.dataOraEsitoField
End Get
Set
If (dataOraEsitoField.Equals(value) <> true) Then
Me.dataOraEsitoField = value
Me.OnPropertyChanged("DataOraEsito")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

Public Property Esito() As EsitoTrasferimentoFTP_Type
Get
Return Me.esitoField
End Get
Set
If (esitoField.Equals(value) <> true) Then
Me.esitoField = value
Me.OnPropertyChanged("Esito")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Public Enum EsitoTrasferimentoFTP_Type

'''<remarks/>
ET01

'''<remarks/>
ET02
End Enum

Partial Public Class NumeroFile_Type
Inherits EntityBase(Of NumeroFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private fileField As List(Of TipoFile_Type)

'''<summary>
'''NumeroFile_Type class constructor
'''</summary>
Public Sub New()
MyBase.New
Me.fileField = New List(Of TipoFile_Type)()
End Sub

Public Property File() As List(Of TipoFile_Type)
Get
Return Me.fileField
End Get
Set
If (Not (Me.fileField) Is Nothing) Then
If (fileField.Equals(value) <> true) Then
Me.fileField = value
Me.OnPropertyChanged("File")
End If
Else
Me.fileField = value
Me.OnPropertyChanged("File")
End If
End Set
End Property
End Class
End Namespace


No problem with the serialization, but I can not use the deserialization. I tried to use it with:



Dim fileEsito As New Classe_FtpTypes.EsitoFTP_Type


but fileEsito gives me only SerializzaXml and SalvaXml. No chance to use DeserializzaXml and CaricaXml.



Thanks in advance for any help.










share|improve this question
























  • Because DeserializzaXml is a Shared function and as such must be called on Classe_FtpTypes.EsitoFTP_Type, not on fileEsito?
    – GSerg
    Nov 12 at 17:24












  • Your serialize function and de-serialize function must use the same 'T' for the code to work. From what I see in the code, "T" is not the same.
    – jdweng
    Nov 12 at 19:28










  • Thank you, I was able to use CaricaXml using: Dim fileEsito As Classe_FtpTypes.EsitoFTP_Type = Classe_FtpTypes.EsitoFTP_Type.CaricaXml(file_destinazione)
    – Jonathan Quid
    Nov 14 at 14:49















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am new to serialization/deserialization in VB.net. I have generated this class with xsd2code:



Namespace Classe_FtpTypes

#Region "Base entity class"
Partial Public Class EntityBase(Of T)
Implements System.ComponentModel.INotifyPropertyChanged

Private Shared sSerializer As System.Xml.Serialization.XmlSerializer

Private Shared ReadOnly Property Serializer() As System.Xml.Serialization.XmlSerializer
Get
If (sSerializer Is Nothing) Then
sSerializer = New System.Xml.Serialization.XmlSerializer(GetType(T))
End If
Return sSerializer
End Get
End Property

Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Overridable Sub OnPropertyChanged(ByVal propertyName As String)
Dim handler As System.ComponentModel.PropertyChangedEventHandler = Me.PropertyChangedEvent
If (Not (handler) Is Nothing) Then
handler(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
End If
End Sub

#Region "Serialize/Deserialize"
'''<summary>
'''Serializes current EntityBase object into an XML document
'''</summary>
'''<returns>string XML value</returns>
Public Overloads Overridable Function SerializzaXml(ByVal encoding As System.Text.Encoding) As String
Dim streamReader As System.IO.StreamReader = Nothing
Dim memoryStream As System.IO.MemoryStream = Nothing
Try
memoryStream = New System.IO.MemoryStream()
Dim xmlWriterSettings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
xmlWriterSettings.Encoding = encoding
xmlWriterSettings.Indent = True
Dim xmlWriter As System.Xml.XmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings)
Serializer.Serialize(xmlWriter, Me)
memoryStream.Seek(0, System.IO.SeekOrigin.Begin)
streamReader = New System.IO.StreamReader(memoryStream)
Return streamReader.ReadToEnd
Finally
If (Not (streamReader) Is Nothing) Then
streamReader.Dispose
End If
If (Not (memoryStream) Is Nothing) Then
memoryStream.Dispose
End If
End Try
End Function

Public Overloads Overridable Function SerializzaXml() As String
Return SerializzaXml(Encoding.UTF8)
End Function

'''<summary>
'''Deserializes workflow markup into an EntityBase object
'''</summary>
'''<param name="xml">string workflow markup to deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = DeserializzaXml(xml)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return DeserializzaXml(xml, obj, exception)
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String) As T
Dim stringReader As System.IO.StringReader = Nothing
Try
stringReader = New System.IO.StringReader(xml)
Return CType(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)),T)
Finally
If (Not (stringReader) Is Nothing) Then
stringReader.Dispose
End If
End Try
End Function

'''<summary>
'''Serializes current EntityBase object into file
'''</summary>
'''<param name="fileName">full path of outupt xml file</param>
'''<param name="exception">output Exception value if failed</param>
'''<returns>true if can serialize and save into file; otherwise, false</returns>
Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef exception As System.Exception) As Boolean
exception = Nothing
Try
SalvaXml(fileName, encoding)
Return true
Catch e As System.Exception
exception = e
Return false
End Try
End Function

Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByRef exception As System.Exception) As Boolean
Return SalvaXml(fileName, Encoding.UTF8, exception)
End Function

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String)
SalvaXml(fileName, Encoding.UTF8)
End Sub

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding)
Dim streamWriter As System.IO.StreamWriter = Nothing
Try
Dim xmlString As String = SerializzaXml(encoding)
streamWriter = New System.IO.StreamWriter(fileName, false, Encoding.UTF8)
streamWriter.WriteLine(xmlString)
streamWriter.Close
Finally
If (Not (streamWriter) Is Nothing) Then
streamWriter.Dispose
End If
End Try
End Sub

'''<summary>
'''Deserializes xml markup from file into an EntityBase object
'''</summary>
'''<param name="fileName">string xml file to load and deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = CaricaXml(fileName, encoding)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
Return CaricaXml(fileName, Encoding.UTF8, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return CaricaXml(fileName, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String) As T
Return CaricaXml(fileName, Encoding.UTF8)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding) As T
Dim file As System.IO.FileStream = Nothing
Dim sr As System.IO.StreamReader = Nothing
Try
file = New System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read)
sr = New System.IO.StreamReader(file, encoding)
Dim xmlString As String = sr.ReadToEnd
sr.Close
file.Close
Return DeserializzaXml(xmlString)
Finally
If (Not (file) Is Nothing) Then
file.Dispose
End If
If (Not (sr) Is Nothing) Then
sr.Dispose
End If
End Try
End Function
#End Region
End Class
#End Region

Partial Public Class QuadraturaFTP_Type
Inherits EntityBase(Of QuadraturaFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraCreazioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroFileField As List(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.numeroFileField = New List(Of TipoFile_Type)()
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraCreazione() As Date
Get
Return Me.dataOraCreazioneField
End Get
Set
If (dataOraCreazioneField.Equals(value) <> true) Then
Me.dataOraCreazioneField = value
Me.OnPropertyChanged("DataOraCreazione")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

<System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
System.Xml.Serialization.XmlArrayItemAttribute("File", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=false)> _
Public Property NumeroFile() As List(Of TipoFile_Type)
Get
Return Me.numeroFileField
End Get
Set
If (Not (Me.numeroFileField) Is Nothing) Then
If (numeroFileField.Equals(value) <> true) Then
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
Else
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Partial Public Class TipoFile_Type
Inherits EntityBase(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private tipoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroField As String

Public Property Tipo() As String
Get
Return Me.tipoField
End Get
Set
If (Not (Me.tipoField) Is Nothing) Then
If (tipoField.Equals(value) <> true) Then
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
Else
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
End Set
End Property

Public Property Numero() As String
Get
Return Me.numeroField
End Get
Set
If (Not (Me.numeroField) Is Nothing) Then
If (numeroField.Equals(value) <> true) Then
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
Else
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
End Set
End Property
End Class

Partial Public Class EsitoFTP_Type
Inherits EntityBase(Of EsitoFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraRicezioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraEsitoField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private esitoField As EsitoTrasferimentoFTP_Type

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraRicezione() As Date
Get
Return Me.dataOraRicezioneField
End Get
Set
If (dataOraRicezioneField.Equals(value) <> true) Then
Me.dataOraRicezioneField = value
Me.OnPropertyChanged("DataOraRicezione")
End If
End Set
End Property

Public Property DataOraEsito() As Date
Get
Return Me.dataOraEsitoField
End Get
Set
If (dataOraEsitoField.Equals(value) <> true) Then
Me.dataOraEsitoField = value
Me.OnPropertyChanged("DataOraEsito")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

Public Property Esito() As EsitoTrasferimentoFTP_Type
Get
Return Me.esitoField
End Get
Set
If (esitoField.Equals(value) <> true) Then
Me.esitoField = value
Me.OnPropertyChanged("Esito")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Public Enum EsitoTrasferimentoFTP_Type

'''<remarks/>
ET01

'''<remarks/>
ET02
End Enum

Partial Public Class NumeroFile_Type
Inherits EntityBase(Of NumeroFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private fileField As List(Of TipoFile_Type)

'''<summary>
'''NumeroFile_Type class constructor
'''</summary>
Public Sub New()
MyBase.New
Me.fileField = New List(Of TipoFile_Type)()
End Sub

Public Property File() As List(Of TipoFile_Type)
Get
Return Me.fileField
End Get
Set
If (Not (Me.fileField) Is Nothing) Then
If (fileField.Equals(value) <> true) Then
Me.fileField = value
Me.OnPropertyChanged("File")
End If
Else
Me.fileField = value
Me.OnPropertyChanged("File")
End If
End Set
End Property
End Class
End Namespace


No problem with the serialization, but I can not use the deserialization. I tried to use it with:



Dim fileEsito As New Classe_FtpTypes.EsitoFTP_Type


but fileEsito gives me only SerializzaXml and SalvaXml. No chance to use DeserializzaXml and CaricaXml.



Thanks in advance for any help.










share|improve this question















I am new to serialization/deserialization in VB.net. I have generated this class with xsd2code:



Namespace Classe_FtpTypes

#Region "Base entity class"
Partial Public Class EntityBase(Of T)
Implements System.ComponentModel.INotifyPropertyChanged

Private Shared sSerializer As System.Xml.Serialization.XmlSerializer

Private Shared ReadOnly Property Serializer() As System.Xml.Serialization.XmlSerializer
Get
If (sSerializer Is Nothing) Then
sSerializer = New System.Xml.Serialization.XmlSerializer(GetType(T))
End If
Return sSerializer
End Get
End Property

Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Overridable Sub OnPropertyChanged(ByVal propertyName As String)
Dim handler As System.ComponentModel.PropertyChangedEventHandler = Me.PropertyChangedEvent
If (Not (handler) Is Nothing) Then
handler(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
End If
End Sub

#Region "Serialize/Deserialize"
'''<summary>
'''Serializes current EntityBase object into an XML document
'''</summary>
'''<returns>string XML value</returns>
Public Overloads Overridable Function SerializzaXml(ByVal encoding As System.Text.Encoding) As String
Dim streamReader As System.IO.StreamReader = Nothing
Dim memoryStream As System.IO.MemoryStream = Nothing
Try
memoryStream = New System.IO.MemoryStream()
Dim xmlWriterSettings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
xmlWriterSettings.Encoding = encoding
xmlWriterSettings.Indent = True
Dim xmlWriter As System.Xml.XmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings)
Serializer.Serialize(xmlWriter, Me)
memoryStream.Seek(0, System.IO.SeekOrigin.Begin)
streamReader = New System.IO.StreamReader(memoryStream)
Return streamReader.ReadToEnd
Finally
If (Not (streamReader) Is Nothing) Then
streamReader.Dispose
End If
If (Not (memoryStream) Is Nothing) Then
memoryStream.Dispose
End If
End Try
End Function

Public Overloads Overridable Function SerializzaXml() As String
Return SerializzaXml(Encoding.UTF8)
End Function

'''<summary>
'''Deserializes workflow markup into an EntityBase object
'''</summary>
'''<param name="xml">string workflow markup to deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = DeserializzaXml(xml)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return DeserializzaXml(xml, obj, exception)
End Function

Public Overloads Shared Function DeserializzaXml(ByVal xml As String) As T
Dim stringReader As System.IO.StringReader = Nothing
Try
stringReader = New System.IO.StringReader(xml)
Return CType(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)),T)
Finally
If (Not (stringReader) Is Nothing) Then
stringReader.Dispose
End If
End Try
End Function

'''<summary>
'''Serializes current EntityBase object into file
'''</summary>
'''<param name="fileName">full path of outupt xml file</param>
'''<param name="exception">output Exception value if failed</param>
'''<returns>true if can serialize and save into file; otherwise, false</returns>
Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef exception As System.Exception) As Boolean
exception = Nothing
Try
SalvaXml(fileName, encoding)
Return true
Catch e As System.Exception
exception = e
Return false
End Try
End Function

Public Overloads Overridable Function SalvaXml(ByVal fileName As String, ByRef exception As System.Exception) As Boolean
Return SalvaXml(fileName, Encoding.UTF8, exception)
End Function

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String)
SalvaXml(fileName, Encoding.UTF8)
End Sub

Public Overloads Overridable Sub SalvaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding)
Dim streamWriter As System.IO.StreamWriter = Nothing
Try
Dim xmlString As String = SerializzaXml(encoding)
streamWriter = New System.IO.StreamWriter(fileName, false, Encoding.UTF8)
streamWriter.WriteLine(xmlString)
streamWriter.Close
Finally
If (Not (streamWriter) Is Nothing) Then
streamWriter.Dispose
End If
End Try
End Sub

'''<summary>
'''Deserializes xml markup from file into an EntityBase object
'''</summary>
'''<param name="fileName">string xml file to load and deserialize</param>
'''<param name="obj">Output EntityBase object</param>
'''<param name="exception">output Exception value if deserialize failed</param>
'''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef obj As T, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = CType(Nothing, T)
Try
obj = CaricaXml(fileName, encoding)
Return true
Catch ex As System.Exception
exception = ex
Return false
End Try
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T, ByRef exception As System.Exception) As Boolean
Return CaricaXml(fileName, Encoding.UTF8, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByRef obj As T) As Boolean
Dim exception As System.Exception = Nothing
Return CaricaXml(fileName, obj, exception)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String) As T
Return CaricaXml(fileName, Encoding.UTF8)
End Function

Public Overloads Shared Function CaricaXml(ByVal fileName As String, ByVal encoding As System.Text.Encoding) As T
Dim file As System.IO.FileStream = Nothing
Dim sr As System.IO.StreamReader = Nothing
Try
file = New System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read)
sr = New System.IO.StreamReader(file, encoding)
Dim xmlString As String = sr.ReadToEnd
sr.Close
file.Close
Return DeserializzaXml(xmlString)
Finally
If (Not (file) Is Nothing) Then
file.Dispose
End If
If (Not (sr) Is Nothing) Then
sr.Dispose
End If
End Try
End Function
#End Region
End Class
#End Region

Partial Public Class QuadraturaFTP_Type
Inherits EntityBase(Of QuadraturaFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraCreazioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroFileField As List(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.numeroFileField = New List(Of TipoFile_Type)()
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraCreazione() As Date
Get
Return Me.dataOraCreazioneField
End Get
Set
If (dataOraCreazioneField.Equals(value) <> true) Then
Me.dataOraCreazioneField = value
Me.OnPropertyChanged("DataOraCreazione")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

<System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
System.Xml.Serialization.XmlArrayItemAttribute("File", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=false)> _
Public Property NumeroFile() As List(Of TipoFile_Type)
Get
Return Me.numeroFileField
End Get
Set
If (Not (Me.numeroFileField) Is Nothing) Then
If (numeroFileField.Equals(value) <> true) Then
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
Else
Me.numeroFileField = value
Me.OnPropertyChanged("NumeroFile")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Partial Public Class TipoFile_Type
Inherits EntityBase(Of TipoFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private tipoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private numeroField As String

Public Property Tipo() As String
Get
Return Me.tipoField
End Get
Set
If (Not (Me.tipoField) Is Nothing) Then
If (tipoField.Equals(value) <> true) Then
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
Else
Me.tipoField = value
Me.OnPropertyChanged("Tipo")
End If
End Set
End Property

Public Property Numero() As String
Get
Return Me.numeroField
End Get
Set
If (Not (Me.numeroField) Is Nothing) Then
If (numeroField.Equals(value) <> true) Then
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
Else
Me.numeroField = value
Me.OnPropertyChanged("Numero")
End If
End Set
End Property
End Class

Partial Public Class EsitoFTP_Type
Inherits EntityBase(Of EsitoFTP_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private identificativoNodoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraRicezioneField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private dataOraEsitoField As Date

<EditorBrowsable(EditorBrowsableState.Never)> _
Private nomeSupportoField As String

<EditorBrowsable(EditorBrowsableState.Never)> _
Private esitoField As EsitoTrasferimentoFTP_Type

<EditorBrowsable(EditorBrowsableState.Never)> _
Private versioneField As String

Public Sub New()
MyBase.New
Me.versioneField = "2.0"
End Sub

Public Property IdentificativoNodo() As String
Get
Return Me.identificativoNodoField
End Get
Set
If (Not (Me.identificativoNodoField) Is Nothing) Then
If (identificativoNodoField.Equals(value) <> true) Then
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
Else
Me.identificativoNodoField = value
Me.OnPropertyChanged("IdentificativoNodo")
End If
End Set
End Property

Public Property DataOraRicezione() As Date
Get
Return Me.dataOraRicezioneField
End Get
Set
If (dataOraRicezioneField.Equals(value) <> true) Then
Me.dataOraRicezioneField = value
Me.OnPropertyChanged("DataOraRicezione")
End If
End Set
End Property

Public Property DataOraEsito() As Date
Get
Return Me.dataOraEsitoField
End Get
Set
If (dataOraEsitoField.Equals(value) <> true) Then
Me.dataOraEsitoField = value
Me.OnPropertyChanged("DataOraEsito")
End If
End Set
End Property

Public Property NomeSupporto() As String
Get
Return Me.nomeSupportoField
End Get
Set
If (Not (Me.nomeSupportoField) Is Nothing) Then
If (nomeSupportoField.Equals(value) <> true) Then
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
Else
Me.nomeSupportoField = value
Me.OnPropertyChanged("NomeSupporto")
End If
End Set
End Property

Public Property Esito() As EsitoTrasferimentoFTP_Type
Get
Return Me.esitoField
End Get
Set
If (esitoField.Equals(value) <> true) Then
Me.esitoField = value
Me.OnPropertyChanged("Esito")
End If
End Set
End Property

Public Property versione() As String
Get
Return Me.versioneField
End Get
Set
If (Not (Me.versioneField) Is Nothing) Then
If (versioneField.Equals(value) <> true) Then
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
Else
Me.versioneField = value
Me.OnPropertyChanged("versione")
End If
End Set
End Property
End Class

Public Enum EsitoTrasferimentoFTP_Type

'''<remarks/>
ET01

'''<remarks/>
ET02
End Enum

Partial Public Class NumeroFile_Type
Inherits EntityBase(Of NumeroFile_Type)

<EditorBrowsable(EditorBrowsableState.Never)> _
Private fileField As List(Of TipoFile_Type)

'''<summary>
'''NumeroFile_Type class constructor
'''</summary>
Public Sub New()
MyBase.New
Me.fileField = New List(Of TipoFile_Type)()
End Sub

Public Property File() As List(Of TipoFile_Type)
Get
Return Me.fileField
End Get
Set
If (Not (Me.fileField) Is Nothing) Then
If (fileField.Equals(value) <> true) Then
Me.fileField = value
Me.OnPropertyChanged("File")
End If
Else
Me.fileField = value
Me.OnPropertyChanged("File")
End If
End Set
End Property
End Class
End Namespace


No problem with the serialization, but I can not use the deserialization. I tried to use it with:



Dim fileEsito As New Classe_FtpTypes.EsitoFTP_Type


but fileEsito gives me only SerializzaXml and SalvaXml. No chance to use DeserializzaXml and CaricaXml.



Thanks in advance for any help.







xml vb.net deserialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 17:23









GSerg

58.6k14100217




58.6k14100217










asked Nov 12 at 14:33









Jonathan Quid

12




12












  • Because DeserializzaXml is a Shared function and as such must be called on Classe_FtpTypes.EsitoFTP_Type, not on fileEsito?
    – GSerg
    Nov 12 at 17:24












  • Your serialize function and de-serialize function must use the same 'T' for the code to work. From what I see in the code, "T" is not the same.
    – jdweng
    Nov 12 at 19:28










  • Thank you, I was able to use CaricaXml using: Dim fileEsito As Classe_FtpTypes.EsitoFTP_Type = Classe_FtpTypes.EsitoFTP_Type.CaricaXml(file_destinazione)
    – Jonathan Quid
    Nov 14 at 14:49




















  • Because DeserializzaXml is a Shared function and as such must be called on Classe_FtpTypes.EsitoFTP_Type, not on fileEsito?
    – GSerg
    Nov 12 at 17:24












  • Your serialize function and de-serialize function must use the same 'T' for the code to work. From what I see in the code, "T" is not the same.
    – jdweng
    Nov 12 at 19:28










  • Thank you, I was able to use CaricaXml using: Dim fileEsito As Classe_FtpTypes.EsitoFTP_Type = Classe_FtpTypes.EsitoFTP_Type.CaricaXml(file_destinazione)
    – Jonathan Quid
    Nov 14 at 14:49


















Because DeserializzaXml is a Shared function and as such must be called on Classe_FtpTypes.EsitoFTP_Type, not on fileEsito?
– GSerg
Nov 12 at 17:24






Because DeserializzaXml is a Shared function and as such must be called on Classe_FtpTypes.EsitoFTP_Type, not on fileEsito?
– GSerg
Nov 12 at 17:24














Your serialize function and de-serialize function must use the same 'T' for the code to work. From what I see in the code, "T" is not the same.
– jdweng
Nov 12 at 19:28




Your serialize function and de-serialize function must use the same 'T' for the code to work. From what I see in the code, "T" is not the same.
– jdweng
Nov 12 at 19:28












Thank you, I was able to use CaricaXml using: Dim fileEsito As Classe_FtpTypes.EsitoFTP_Type = Classe_FtpTypes.EsitoFTP_Type.CaricaXml(file_destinazione)
– Jonathan Quid
Nov 14 at 14:49






Thank you, I was able to use CaricaXml using: Dim fileEsito As Classe_FtpTypes.EsitoFTP_Type = Classe_FtpTypes.EsitoFTP_Type.CaricaXml(file_destinazione)
– Jonathan Quid
Nov 14 at 14:49



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264349%2fhow-to-deserialize-in-vb-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264349%2fhow-to-deserialize-in-vb-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?