vb 对象到xml的存入和读取

    Public Function SaveConfigToFile(Of T)(ByVal strFilePath As String, ByVal data As T) As Boolean
        Dim objStreamWriter As New StreamWriter(strFilePath)
        Try
            Dim xsSerialize As New XmlSerializer(data.GetType)
            xsSerialize.Serialize(objStreamWriter, data)
            objStreamWriter.Close()
        Catch ex As Exception
            objStreamWriter.Close()
            Return False
        End Try
        Return True
    End Function

    Public Function ReadConfigFromFile(Of T)(ByVal strFilePath As String, ByRef data As T) As Boolean
        Dim objStreamReader As New StreamReader(strFilePath)
        Try
            Dim xsDeserialize As New XmlSerializer(data.GetType)
            data = xsDeserialize.Deserialize(objStreamReader)
            objStreamReader.Close()
        Catch ex As Exception
            objStreamReader.Close()
            Return False
        End Try

        Return True
    End Function

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/107242471
VB