all groups > dotnet xml > january 2006 >
You're in the

dotnet xml

group:

How to: Omit Deafulted Elements from XML When Serializing


How to: Omit Deafulted Elements from XML When Serializing Charles Law
1/28/2006 6:36:10 PM
dotnet xml:
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

Re: How to: Omit Deafulted Elements from XML When Serializing Clive Dixon
1/30/2006 2:52:56 PM
(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.

[quoted text, click to view]

Re: How to: Omit Deafulted Elements from XML When Serializing Charles Law
1/30/2006 3:52:43 PM
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" <clived.noluncheonmeat@digita.noluncheonmeat.com> wrote in
message news:eif4czaJGHA.3176@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Re: How to: Omit Deafulted Elements from XML When Serializing Clive Dixon
1/30/2006 4:32:26 PM
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

[quoted text, click to view]

AddThis Social Bookmark Button