如何在VB.NET中反序列化

时间:2018-11-12 14:33:31

标签: xml vb.net deserialization

我是VB.net中序列化/反序列化的新手。我已经使用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

序列化没问题,但是我不能使用反序列化。我尝试将其用于:

Dim fileEsito As New Classe_FtpTypes.EsitoFTP_Type

但是fileEsito仅给我SerializzaXmlSalvaXml。没有机会使用DeserializzaXmlCaricaXml

在此先感谢您的帮助。

0 个答案:

没有答案