需要将对象转换为XML并收到错误

时间:2010-02-15 19:15:55

标签: .net xml vb.net xml-serialization

我有这个课程:

  <Serializable()> _

   Public Class Bonder
    ''' <summary>
    ''' General information
    ''' </summary>
    ''' <remarks></remarks>
    Public BonderName As String
    Public Serial_Number As String
    Public System_Type As String
    Public DM_Version As String
    Public RTS_Build As String
    Public RTS_Label As String
    Public CPU_Boot_Rom As String
    Public BondHead_1 As String = ""
    Public BondHead_2 As String = ""
    Public IP1 As String
    Public IP2 As String
    Public LoadedLeadFrameSetup As String
    Public LoadedMagazineHandler As String
    Public LoadedProcessProgram As String

    ''' <summary>
    ''' Configuration Information
    ''' </summary>
    ''' <remarks></remarks>
    Public ConfigurationKVP As New ArrayList


    ''' <summary>
    ''' Devices on the Bonder
    ''' </summary>
    ''' <remarks></remarks>
    Public DevicesOnBonder As New ArrayList

    ''' <summary>
    ''' RTS server information
    ''' </summary>
    ''' <remarks></remarks>
    Public ServerInfo As New ArrayList


    ''' <summary>
    ''' RTS Options selected
    ''' </summary>
    ''' <remarks></remarks>
    Public Options As New ArrayList

End Class`

这是我的序列化代码并将其转换为XML格式的代码:

  Dim serializer As XmlSerializer
    serializer = New XmlSerializer(currentBonderSetup.GetType)
    Dim tw As New StreamWriter("c:\book1.xml")
    serializer.Serialize(tw, currentBonderSetup)
    tw.Close()

我哪里错了?我认为问题来自ArrayLists,但我不知道如何解决它。 ArrayLists包含具有Seri​​alizeable属性的其他对象。

以下是其他类

<Serializable()> _
Public Class Configs
    Public Item As String
    Public Value As String

    Public Sub New(ByVal key As String, ByVal result As String)
        Item = key
        Value = result
    End Sub

End Class

<Serializable()> _
Public Class BonderDevices
    Public Device_Type As String
    Public Instance As String
    Public Board_Rev As String
    Public Software_Ver As String
    Public Status As String
    Public Data As String

End Class
<Serializable()> _
Public Class ServerInfo
    Public Type As String
    Public Value As String
End Class

2 个答案:

答案 0 :(得分:2)

将参数构造函数添加到Configs

   Public Sub New()

   End Sub

通过将类型数组传递给构造函数,可以考虑给序列化程序一个关于类型的提示。

答案 1 :(得分:1)

尝试构建序列化程序......

var s = new XmlSerializer(typeof(Bonder), new Type[]{typeof(Configs),typeof(BonderDevices),typeof(ServerInfo)});

并且,是的,您需要在所有类型上使用默认的无参数构造函数。

VB中的

Dim s = New XmlSerializer(GetType(Bonder), New Type() {GetType(Configs), GetType(BonderDevices), GetType(ServerInfo)})