在序列化期间发送空值

时间:2013-11-19 18:45:08

标签: vb.net

尝试确定最佳的seralize方式,这样如果没有输入中间名,我可以发送空值

我的xml:

<Contact>
    <ID />
    <First_Name />
    <Middle_Name  xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:nil="true" />
    <Last_Name />
    <TelephoneNumbers>
        <TelephoneNumber>
            <Number />
            <IsHome />
            <IsWork />
            <IsCell />
            <ReachableAfterHrs />
        </TelephoneNumber>
    </TelephoneNumbers>
 </Contact>

为序列化/反序列化创建的vb类

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class Contact

    Private idField As String

    Private first_NameField As String

    Private middle_NameField As ContactMiddle_Name

    Private last_NameField As String

    Private telephoneNumbersField As ContactTelephoneNumbers

    '''<remarks/>
    Public Property ID() As String
        Get
            Return Me.idField
        End Get
        Set(value As String)
            Me.idField = Value
        End Set
    End Property

    '''<remarks/>
    Public Property First_Name() As String
        Get
            Return Me.first_NameField
        End Get
        Set(value As String)
            Me.first_NameField = Value
        End Set
    End Property

    '''<remarks/>
    Public Property Middle_Name() As ContactMiddle_Name
        Get
            Return Me.middle_NameField
        End Get
        Set(value As ContactMiddle_Name)
            Me.middle_NameField = Value
        End Set
    End Property

    '''<remarks/>
    Public Property Last_Name() As String
        Get
            Return Me.last_NameField
        End Get
        Set(value As String)
            Me.last_NameField = Value
        End Set
    End Property

    '''<remarks/>
    Public Property TelephoneNumbers() As ContactTelephoneNumbers
        Get
            Return Me.telephoneNumbersField
        End Get
        Set(value As ContactTelephoneNumbers)
            Me.telephoneNumbersField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactMiddle_Name

    Private nilField As Boolean

    '''<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Qualified, [Namespace]:="w3.org/2001/XMLSchema-instance")> _
    Public Property nil() As Boolean
        Get
            Return Me.nilField
        End Get
        Set(value As Boolean)
            Me.nilField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactTelephoneNumbers

    Private telephoneNumberField As ContactTelephoneNumbersTelephoneNumber

    '''<remarks/>
    Public Property TelephoneNumber() As ContactTelephoneNumbersTelephoneNumber
        Get
            Return Me.telephoneNumberField
        End Get
        Set(value As ContactTelephoneNumbersTelephoneNumber)
            Me.telephoneNumberField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactTelephoneNumbersTelephoneNumber

    Private numberField As Object

    Private isHomeField As Object

    '''<remarks/>
    Public Property Number() As Object
        Get
            Return Me.numberField
        End Get
        Set(value As Object)
            Me.numberField = Value
        End Set
    End Property

    '''<remarks/>
    Public Property IsHome() As Object
        Get
            Return Me.isHomeField
        End Get
        Set(value As Object)
            Me.isHomeField = Value
        End Set
    End Property
End Class

反序列化时,输出包含表示空元素而不是空元素的标记。客户希望我们发送null而不是空。

1 个答案:

答案 0 :(得分:0)