By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,899 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,899 IT Pros & Developers. It's quick & easy.

How to: Omit Deafulted Elements from XML When Serializing

P: n/a
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
Jan 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
(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.

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Jan 30 '06 #2

P: n/a
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
"Clive Dixon" <cl*******************@digita.noluncheonmeat.com > wrote in
message news:ei**************@TK2MSFTNGP12.phx.gbl...
(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.

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Jan 30 '06 #3

P: n/a
Charles,

I had quite forgotten about the other methods you discovered - have no idea
whether they still function in the same way in 2.0. The
'propertyNameSpecified' method seems much more flexible though.

If you look in the VS2003 help for XmlSerializer class ('about XMLSerializer
class'), about half way down the page is the following (I suspect the MSDN
documentation for this will be identical):

Another option is to use a special pattern to create a Boolean field
recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the
field. The pattern is created in the form of propertyNameSpecified. For
example, if there is a field named "MyFirstName" you would also create a
field named "MyFirstNameSpecified" that instructs the XmlSerializer whether
or not to generate the XML element named "MyFirstName". This is shown in the
following C# and Visual Basic example.
' Visual Basic
Public Class OptionalOrder
' This field's value shouldn't be serialized
' if it is uninitialized.
Public FirstOrder As String
' Use the XmlIgnoreAttribute to ignore the
' special field named "FirstOrderSpecified".
<System.Xml.Serialization.XmlIgnoreAttribute> _
Public FirstOrderSpecified As Boolean
End Class

"Charles Law" <bl***@nowhere.com> wrote in message
news:OT*************@TK2MSFTNGP11.phx.gbl...
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

Jan 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.