16,722
社区成员




Public Shared Function DeserializingXml_1(Of T)(ByRef instance As T, ByVal path As String) As Boolean
Try
'生成DataContractSerializer
Dim serializer As New DataContractSerializer(GetType(T))
'读入文件
Using fs As FileStream = New FileStream(path, FileMode.Open)
'---这里报错
instance = DirectCast(serializer.ReadObject(fs), T)
End Using
Return True
Catch ex As Exception
XmlSerialize.ErrorMessage = ex.Message
Return False
End Try
End Function
Private Sub deserializingXml(Of Type As Class)(ByRef refInstance As Type, ByVal path As String)
refInstance = Nothing
Dim fs As FileStream = Nothing
Try
Dim sr As New System.Xml.Serialization.XmlSerializer(GetType(Type))
fs = New FileStream(path, FileMode.Open, FileAccess.Read)
Try
refInstance = DirectCast(sr.Deserialize(fs), Type)
Finally
fs.Close()
End Try
Catch
Throw
Finally
If refInstance Is Nothing Then
Console.WriteLine("Error--->Deserialize")
End If
End Try
End Sub
Public Function Load(Of Type As Class)(ByRef instance As Type, ByVal path As String) As Boolean
Try
' 这里调用方法
DeserializingXml(instance, path)
Catch
Return False
End Try
Return True
End Function
If path Is Nothing Then
filePath = "文件名.xml"
Else
filePath = path
End If
Dim loadData As TraceabilityConfig = Nothing
If Not (New XmlSerialize()).Load(loadData, filePath) Then
Return False
End If