Rob*_*ers 5 vb.net serialization xmlserializer
我心中已经建立NotInheritable Serilizer是serilizes我的所有类以及与成功类的列表.直到我构建一个包含类列表的类列表.我正在获取运行时Exeption:生成XML文档时出错.导致完全空的XML :(
这些是我的serilize课程:
<System.Serializable> _
<System.Xml.Serialization.XmlInclude(GetType(StatisticItem))> _
Public Class Statistic
Public StatItem As New list(Of StatisticItem)
'Bla bla bla
end class
<Serializable> _
Public Class StatisticItem
Private stStatPath As String = ""
Private eStatType As StatType = 0
Private iBatchNumber As Int32 = 0
end class
Run Code Online (Sandbox Code Playgroud)
和序列化器:
Public NotInheritable Class XmlSerializer
Public Shared Sub Serialize(Of T)(ByVal obj As T, sConfigFilePath As String)
Dim XmlBuddy As New System.Xml.Serialization.XmlSerializer(GetType(T))
Dim MySettings As New System.Xml.XmlWriterSettings()
MySettings.Indent = True
MySettings.CloseOutput = True
Dim MyWriter As System.Xml.XmlWriter=System.Xml.XmlWriter.Create(sConfigFilePath,MySettings)
XmlBuddy.Serialize(MyWriter,obj)
MyWriter.Flush()
MyWriter.Close()
' ----- OLD CODE FOR SERIALIZE, NEXTLINE IN XML DOESNT WORK ON WIN CE -------,
' B.T.W. Using This code to serilize gives the exact same fault
'Dim XmlBuddy As New System.Xml.Serialization.XmlSerializer(GetType(T))
'Dim objStreamWriter As New StreamWriter(sConfigFilePath)
'XmlBuddy.Serialize(objStreamWriter, obj)
'objStreamWriter.Close()
End Sub
end class
Run Code Online (Sandbox Code Playgroud)
这就是电话:
XmlSerializer.Serialize(Of list(Of Statistic))(StatCollection, CommCtrl.PathStatisticFile)
Run Code Online (Sandbox Code Playgroud)
如果我在StatisticItem中评论列表一切正常.
我想如果我在StatisticItem中实现IXmlSerializable,我可以告诉序列化器如何使其工作,但我在互联网上看到其他代码示例,这没有所有这些努力,我更喜欢一个干净的解决方案,这与大致相同我所有的其他课程.
希望你们其中一个人可以帮助我
是的,解决了!,说实话,我改变了很多小事情,我仍然不知道原因是什么。也许还有一些私人成员。无论如何,也许代码对任何人都有用:
Public Class Statistic
'Properties
Private eStatName As String
Private eStatSort As StatSort
Private StatItem As New list(Of StatisticItem)
Public Property Name() As String
Get
Return eStatName
End Get
Set(ByVal value As String)
eStatName = value
End Set
End Property
'Other public properties
End class
Public Class StatisticItem
Private stStatPath As String = ""
Private eStatType As StatType = 0
Private iBatchNumber As Int32 = 0
Public Property Path() As String
Get
Return stStatPath
End Get
Set(ByVal Value As String)
stStatPath = Value
End Set
End Property
' The other Public Properties
Run Code Online (Sandbox Code Playgroud)
序列化器:
Public NotInheritable Class XmlSerializer
''' <summary>
''' Convert a class state into XML
''' </summary>
''' <typeparam name="T">The type of object</typeparam>
''' <param name="obj">The object to serilize</param>
''' <param name="sConfigFilePath">The path to the XML</param>
Public Shared Sub Serialize(Of T)(ByVal obj As T, sConfigFilePath As String)
Dim XmlBuddy As New System.Xml.Serialization.XmlSerializer(GetType(T))
Dim MySettings As New System.Xml.XmlWriterSettings()
MySettings.Indent = True
MySettings.CloseOutput = True
Dim MyWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sConfigFilePath,MySettings)
XmlBuddy.Serialize(MyWriter,obj)
MyWriter.Flush()
MyWriter.Close()
End Sub
''' <summary>
''' Restore a class state from XML
''' </summary>
''' <typeparam name="T">The type of object</typeparam>
''' <param name="xml">the path to the XML</param>
''' <returns>The object to return</returns>
Public Shared Function Deserialize(Of T)(ByVal xml As String) As T
Dim XmlBuddy As New System.Xml.Serialization.XmlSerializer(GetType(T))
Dim fs As New FileStream(xml, FileMode.Open)
Dim reader As New Xml.XmlTextReader(fs)
If XmlBuddy.CanDeserialize(reader) Then
Dim tempObject As Object = DirectCast(XmlBuddy.Deserialize(reader), T)
reader.Close()
Return tempObject
Else
Return Nothing
End If
End Function
end class
Run Code Online (Sandbox Code Playgroud)
序列化器的调用:
Try
XmlSerializer.Serialize(Of list(Of Statistic))(StatCollection, CommCtrl.PathStatisticFile)
Catch ex As Exception
msgbox(ex.Message)
End Try
Run Code Online (Sandbox Code Playgroud)
反序列化器的调用:
Try
StatCollection = XmlSerializer.Deserialize(Of list(Of Statistic)(CommCtrl.PathStatisticFile)
Catch ex As Exception
msgbox(ex.Message)
end Try
Run Code Online (Sandbox Code Playgroud)