关于一个serializer 的问题

Neusoft06 2014-02-10 03:06:01
方法1
    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

方法2

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


为什么使用方法1会报错--命名空间为空
方法2就没问题
...全文
116 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧