Serialisation - To Display or Not to Display

3 views
Skip to first unread message

dickster

unread,
Oct 31, 2005, 9:26:34 AM10/31/05
to
A general query

Im serialising a person object with the following member variables

Class Person

Private _forenames As String
Private _surname As String
Private _dob As Date
Private _marital_status As MaritalStatus

'Public Get and Set Properties here for each of the above
' I'll not detail them all to save space but Ill show attributes
<XmlElementAttribute(Elementname:="forename")> _
<XmlElementAttribute(Elementname:="surname")> _
<XmlElementAttribute(Elementname:="dob")> _
<XmlElementAttribute(Elementname:="marital_status")> _


Public Sub New()
End New

Public Enum MaritalStatus
Single,
Married
End Enum

End Class

Say I create a new person:

Dim Dickster as New Person
Dickster.Forename = "mike"
Dickster.Surname = "smith"


It will Serialise as

<Person>
<forename>mike</forename>
<surname>smith</surname>
<dob>1900-01-01</dob>
<marital_status>Single>
</Person>

BUT I didnt set the <dob> and <marital_status> node so i dont want them
to show .

So to work round this i wrapped dates and enumerations in individual
classes...
This was grand but a bit OTT...

Then I discovered ....Specified As Boolean

Private _marital_status As MaritalStatus
Private _marital_statusSpecified As Boolean = False


<XmlElementAttribute(ElementName:="marital_status")> _
Public Property marital_status() As MaritalStatus
Get
Return _marital_status
End Get
Set(ByVal Value As MaritalStatus)

_marital_status = Value
If Value Is Nothing Then
_marital_statusSpecified = False
Else
_marital_statusSpecified = True
End If
End Set
End Property

<XmlIgnore()> _
Public Property marital_statusSpecified() As Boolean
Get
Return _marital_statusSpecified
End Get
Set(ByVal Value As Boolean)
_marital_statusSpecified = Value
End Set
End Property

Likewise for Dates and Ints


Is there any thoughts about using this means to hide the serilisation
of unset values ?

Thanks

Dickster

Derek Harmon

unread,
Oct 31, 2005, 7:08:20 PM10/31/05
to
"dickster" <gd...@kerrhenderson.com> wrote in message news:1130768794.0...@g47g2000cwa.googlegroups.com...

> BUT I didnt set the <dob> and <marital_status> node so i dont want them
> to show .
: :

> Then I discovered ....Specified As Boolean
>
> Private _marital_status As MaritalStatus
> Private _marital_statusSpecified As Boolean = False
: :

> Is there any thoughts about using this means to hide the serilisation
> of unset values ?

What you've discovered is the conventional way for handling this
when working with the built-in XmlSerializer.


Derek Harmon


dickster

unread,
Nov 1, 2005, 6:15:42 AM11/1/05
to
Thanks Derek

I had a look on msdn but couldnt really find anything, was wondering
could you point me in the direction of documentation regarding this.

Dickster

Derek Harmon

unread,
Nov 1, 2005, 10:48:45 PM11/1/05
to
"dickster" <gd...@kerrhenderson.com> wrote in message news:1130843742....@f14g2000cwb.googlegroups.com...

> I had a look on msdn but couldnt really find anything, was wondering
> could you point me in the direction of documentation regarding this.

http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlmembermapping.checkspecified


Derek Harmon


Reply all
Reply to author
Forward
0 new messages