正确的方法来序列化包含vb.net/c#中其他元素列表的xml元素

时间:2015-12-14 19:06:49

标签: c# xml vb.net serialization

我想生成一个XML序列化两个vb.net classes我想写保姆联系信息付款(付款是在列表,可以是现金,票,支票),如:

<roor>
    <babysitters>
         <babysitter>
             <ID>1</ID>
             <Name>Diana Swal</Name>
             <Experience>3</Experience>
             <PaymentInfo>
                <bank>BBVA<bank>
                <account>1234567898<account>
             </PaymentInfo>
             <Payment id=1 type="cash" date="2015-06-06">
                 <description>a descr </description>                   
             </Payment>
             <Payment id=2 type="cash" date="2015-07-16">
                 <description>a descr </description>                   
             </Payment>
             <Payment id=3 type="cash" date="2015-07-16">
                 <description>a descr </description>                   
             </Payment>
         </babysitter>
         <babysitter>
             <ID>2</ID>
             <Name>Koryna Tokya</Name>
             <Experience>1</Experience>
             <PaymentInfo>
                <bank>New York<bank>
                <account>123477898<account>
             </PaymentInfo>
             <Payment id=11 type="check" date="2015-01-06">
                 <description>a descr </description>                   
             </Payment>
             <Payment id=22 type="check" date="2015-02-16">
                 <description>a descr </description>                   
             </Payment>
             <Payment id=33 type="cash" date="2015-03-16">
                 <description>a descr </description>                   
             </Payment>
         </babysitter>

    </babysitters>
</roor>

这样做我正在尝试:

<XmlRoot ("roor")> _
Public Class roor
        Public Property Babysitters As List(Of Babysitter)

        Public Sub New()
            Babysitters = New List(Of Babysitter)()
        End Sub
End Class

<Serializable()>
Public Class Babysitter
    Public Property ID As Integer
    Public Property Name As String
    Public Property Experience As Integer
    'how to define payment info properties ?
    ' I guess an extra class is not necesary so instead of
    'defining each one of payment info maybe one property or struct
    'Public Property PaymentInfo As ????
    Public Property PaymentInfo_bank As String
    Public Property PaymentInfo_account As String

    'for the payments I am using an array of payments
    <XmlArrayItem("Payments")>
    Public Property Payments As List(Of Payment)

    Public Sub New()
        Payments = New List(Of Payment)()
    End Sub
End Class


<Serializable()>
    Public Class Payment
        <XmlAttribute("id")>
        Public Property id As Integer
        <XmlAttribute("type")>
        Public Property type As String
        <XmlAttribute("date")>
        Public Property date As String
        Public Property description As String
    End Class

如何在xml中序列化信息?该代码定义是否会生成类似给定的xml?

1 个答案:

答案 0 :(得分:2)

如果您有一个示例xml文件,那么您可以自动生成类以避免出现任何差异。

  1. 使用示例xml文件生成xsd(架构定义) - 例如在Visual Studio(https://msdn.microsoft.com/en-us/library/ms255829.aspx)中。
  2. 使用xsd文件使用xsd.exe创建类(在vb.net或c#中) - 内置于Visual Studio中(https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx