• 主页
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API

关于一个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就没问题
...全文
27 点赞 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
VB
创建于2007-09-28

1.1w+

社区成员

11.0w+

社区内容

VB技术相关讨论,主要为经典vb,即VB6.0
社区公告
暂无公告