How to: Omit Deafulted Elements from XML When Serializing

C

Charles Law

I have a complex object that I am serializing, and I would like to omit
elements that have a default value.

For example, if I have a class as follows

Public Class Test

Private m_Name As String = "George"
Private m_Active As Boolean = False
Private m_Address As String

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Property Active() As Boolean
Get
Return m_Active
End Get
Set(ByVal Value As Boolean)
m_Active = Value
End Set
End Property

Public Property Address() As String
Get
Return m_Address
End Get
Set(ByVal Value As String)
m_Address = Value
End Set
End Property

End Class

If Name is "George" when I serialize the object then I don't want this
element to be included in the output. Similarly, if Active is "False" when
the object is serialized, then I don't want that element included in the
output either. Anything different, and they should be included.

Is there a way to do this, perhaps by tagging the property/element in some
way? I am using VS2003.

TIA

Charles
 
C

Clive Dixon

(Apologies for any bad VB in advance - I'm a C# person)
For each field add a corresponding Boolean field & property suffixed with
Specified (and the property has attribute XmlIgnore), such as:

Public Class Test

Private m_Name As String = "George"
Private m_NameSpecified As Boolean = False

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
m_NameSpecified = True
End Set
End Property

<XmlIgnore()>
Public Property NameSpecified() As Boolean
Get
Return m_NameSpecified
End Get
Set(ByVal Value As Boolean)
m_NameSpecified = Value
End Set
End Property

The framework on serializing each field looks for a field of type Boolean
with the field name suffixed with "Specified". If this field exists and has
value False, the field will not be serialized.

Note that in the original field's Set accessor I set the XXXSpecified field
to True. This means that if I ever set the property then it will get
serialized; if I don't set it, it won't. If you specifically want it so that
if it's not "George" it will be serialized then that is easy enough for you
to change appropriately.
 
C

Charles Law

Hi Clive

That does exactly what I want. Thanks.

I have never seen this documented, can you point me to an MSDN description
of this feature?

Whilst looking myself, I came across another way that seems to work:

<ComponentModel.DefaultValue("George")> _
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Name now does not get serialized when it returns "George". Also,
XmlAttributes.XmlDefaultValue can be used to do the same thing at runtime.
The only concern with this is that MSDN article KB325691 highlights that
Microsoft intend to change this behaviour in the next major version release
of the .NET framework. I haven't checked, but I wonder if this technique no
longer works in .NET 2.0.

Anyway, many thanks again.

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top