all groups > dotnet xml > october 2005 >
You're in the

dotnet xml

group:

Serialisation - To Display or Not to Display


Serialisation - To Display or Not to Display dickster
10/31/2005 6:26:34 AM
dotnet xml:
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
Re: Serialisation - To Display or Not to Display Derek Harmon
10/31/2005 7:08:20 PM
[quoted text, click to view]

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


Derek Harmon

Re: Serialisation - To Display or Not to Display dickster
11/1/2005 3:15:42 AM
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
Re: Serialisation - To Display or Not to Display Derek Harmon
11/1/2005 10:48:45 PM
[quoted text, click to view]

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


Derek Harmon

AddThis Social Bookmark Button