VB.NET – Serialized generic class (no XML help class)

I have a question about serialization of generic classes.

Because I want to use it for all my Configuration (which is serialized as XML) Generic class, so I wrote the following classes:

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports System.Reflection
Imports System.Reflection.Assembly

_
Public Class GenericConfig(Of T)
Public Sub WriteToFile(ByVal FileName As String)
ConfigVersion = ProgrammVersion.ToString
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Create)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(GenericConfig(Of T)) , New Type() {GetType(GenericConfig(Of T))})
serialize.Serialize(XmlFile, Me)
XmlFile.Close()
End Sub

Public Shared Function ReadFromFile(ByVal FileName As String) As GenericConfig(Of T)
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Open)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(GenericConfig(Of T)))
ReadFromFile = serialize.Deserialize(XmlFile)
XmlFile.Close()
Return ReadFromFile
End Function

Public Shared ReadOnly Property ConfigFileName() As String
Get
Return GetExecutingAssembly.Location.Substring(0, GetExecutingAssembly.Location.Length-4) & ".Config.xml"
End Get
End Property
End Class

Public Class ToolConfig
Inherits GenericConfig(Of ToolConfig)

Public Property Key1 As String = "Value 1 "
End Class

The problem I have is that I cannot serialize it because of the following error in the line

serialize.Serialize( XmlFile, Me)

{“GenericConfigClass.ToolConfig type is not expected. Use XmlInclude or SoapInclude attributes to specify static unknown types.”}

If you use XMLSerializer on a derived class, I I have googled and found a lot of articles related to the error. I did not do it here. I use it on generic classes.

In addition to creating XML helper classes like this partner here Besides, there are other methods:
http://www.codeproject.com/Articles/35925/Generic-XML-Serialization-Methods

Thank you,
Wolfgang

Update:
This is what I said (short example)

Imports System.IO

Public Class frmMain
Dim Config As ToolConfig

Private Sub btnConfigInit_Click(sender As Object, e As EventArgs) Handles btnConfigInit.Click
'----- ------------ read config file -----------------
If File.Exists(ToolConfig.ConfigFileName) Then
Config = ToolConfig.ReadFromFile(ToolConfig.ConfigFileName)
Else
Config = New ToolConfig
End If
Config.WriteToFile(ToolConfig.ConfigFileName)

MsgBox (Config.Key1)
End Sub
End Class

Yes, You are using a derived class: ToolConfig. Let it know through XmlInclude, you should be fine:

Add to GenericConfig.

Another One solution should be to simply use Me.GetType() instead of GetType(GenericConfig(Of T)) when creating an XmlSerializer instance:

Dim serialize As XmlSerializer = New XmlSerializ er(Me.GetType())

In order to be able to deserialize successfully, you must make the shared ReadFromFile method an instance method or make it a generic method.

If you want to keep sharing The deserialization method, you can do this:

Public Class GenericConfig '<- No (Of T) here! But in the line below.
Public Shared Function ReadFromFile(Of T)(ByVal FileName As String) As T
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Open)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(T))
ReadFromFile = serialize.Deserialize(XmlFile)
XmlFile.Close()
Return ReadFromFile
End Function
End Class

Usage:

GenericConfig.ReadFromFile(Of ToolConfig)(filename)

I have a question about serialization of generic classes.

Because I want to use generic classes for all my Configuration (which is serialized as XML), I wrote the following classes:

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports System.Reflection
Imports System.Reflection.Assembly

_
Public Class GenericCo nfig(Of T)
Public Sub WriteToFile(ByVal FileName As String)
ConfigVersion = ProgrammVersion.ToString
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Create)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(GenericConfig(Of T)), New Type() {GetType(GenericConfig(Of T))))
serialize.Serialize(XmlFile, Me)
XmlFile.Close ()
End Sub

Public Shared Function ReadFromFile(ByVal FileName As String) As GenericConfig(Of T)
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Open)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(GenericConfig(Of T)))
ReadFromFile = serialize.Deserialize(XmlFile)
XmlFile.Close()
Return ReadFromFile
End Function

Public Shared ReadOnly Property ConfigFileName() As String
Get
Return GetExecutingAssembly.Location.Su bstring(0, GetExecutingAssembly.Location.Length-4) & ".Config.xml"
End Get
End Property
End Class

Public Class ToolConfig
Inherits GenericConfig(Of ToolConfig)

Public Property Key1 As String = "Value 1"
End Class

The problem I have is that I cannot serialize it , Because of the following error in the line

serialize.Serialize(XmlFile, Me)

{“GenericConfigClass.ToolConfig type is not expected. Use XmlInclude or SoapInclude attributes Specify static unknown type. “}

If you use XMLSerializer on derived classes, I have googled and found many articles related to the error. I did not do it here. I am in generics Use it on the class.

Besides creating an XML helper class like this partner here, there are other methods:
http://www.codeproject.com/Articles/35925/ Generic-XML-Serialization-Methods

Thank you,
Wolfgang

Update:
This is what I said (short example)

< p>

Imports System.IO

Public Class frmMain
Dim Config As ToolConfig

Private Sub btnConfigInit_Click(sender As Object, e As EventArgs) Handles btnConfigInit.Click
'----------------- read config file -----------------
If File.Exist s(ToolConfig.ConfigFileName) Then
Config = ToolConfig.ReadFromFile(ToolConfig.ConfigFileName)
Else
Config = New ToolConfig
End If
Config.WriteToFile(ToolConfig. ConfigFileName)

MsgBox(Config.Key1)
End Sub
End Class

Yes, you Derived class is being used: ToolConfig. Let it know through XmlInclude, you should be fine:

Add to GenericConfig.

Another The solution should be to simply use Me.GetType() instead of GetType(GenericConfig(Of T)) when creating an XmlSerializer instance:

Dim serialize As XmlSerializer = New XmlSerializer( Me.GetType())

In order to be able to deserialize successfully, you must make the shared ReadFromFile method an instance method or make it a generic method.

If you want to keep the shared reverse Serialization method, you can do this:

Public Class GenericConfig '<- No (Of T) here! But in the line below.
Public Shared Function ReadFromFile( Of T)(ByVal FileName As String) As T
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Open)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(T))
ReadFromFile = serialize.Deserialize(XmlFile)
XmlFile.Close()
Return ReadFromFile
End Function
End Class

Usage:

GenericConfig.ReadFromFile(Of ToolConfig)(filename)

Leave a Comment

Your email address will not be published.