vb.net xml反序列化

时间:2017-05-10 15:22:15

标签: xml vb.net serialization

我有以下XML,对于我的生活,我无法弄清楚反序列化的类是什么样的。

<?xml version="1.0" encoding="utf-16"?>
<Root>
    <AL>
       <AcctName>Test</AcctName>
       <RetentionCodes>
           <RetentionCode Del="N">ZZZZZ</RetentionCode>
           <RetentionCode Del="N">XXXXX</RetentionCode>
       </RetentionCodes>
    </AL>
</Root>

到目前为止,我有一个课程:

<Serializable(), XmlRoot("Root")>
Public Class clsRecord Inherits clsFunctions(Of clsRecord)

Private m_AL As clsAL = Nothing
 <XmlElement("AL")>
 Public Property AL() As clsAL
    Get
        Return m_AL
    End Get
    Set(ByVal value As clsAL)
        m_AL = value
    End Set
 End Property

 Public Sub New()
 End Sub

 End Class

Public Class clsAL Inherits clsFunctions(Of clsAL)

 Private m_AcctName As String = String.Empty
 Public Property AcctName() As String
    Get
        Return m_AcctName
    End Get
    Set(ByVal value As String)
        m_AcctName = value.ToString
    End Set
 End Property

<XmlArrayItem("RetentionCode")>
Public Property RetentionCodes As List(Of String)

End Class

我可以获取Node值,但我不确定如何将DEL属性放入类中,而不是创建单独的RetentionCode类。

1 个答案:

答案 0 :(得分:0)

RetentionCode必须是一个类。使用<XmlArrayItem("RetentionCode")>制作一份RetentionCode列表。

<XmlRoot("Root")>
Public Class clsRecord Inherits clsFunctions(Of clsRecord)
    Private m_AL As clsAL = Nothing
    <XmlElement>
    Public Property AL() As clsAL
        Get
            Return m_AL
        End Get
        Set(ByVal value As clsAL)
            m_AL = value
        End Set
    End Property
End Class

Public Class clsAL Inherits clsFunctions(Of clsAL)
    Private m_AcctName As String = String.Empty
    <XmlElement>
    Public Property AcctName() As String
        Get
            Return m_AcctName
        End Get
        Set(ByVal value As String)
            m_AcctName = value.ToString
        End Set
    End Property
    <XmlArrayItem("RetentionCode")>
    Public Property RetentionCodes As List(Of RetentionCode)
End Class

Public Class RetentionCode
    <XmlAttribute>
    Public Property Del As String
    <XmlTextAttribute>
    Public Property Value As String
End Class