473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to: Omit Deafulted Elements from XML When Serializing

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
3 2314
(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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jonas Hei | last post by:
Can someone please clarify the in which XML standard (and various parsers) handle sequence of elements in XML? 1. Are the following two documents same (syntactically and sematically)? a. <Data>...
7
by: grwalker | last post by:
I have some classes that have the <Serializable()> attribute applied, which of course by default serializes the class properties as elements. What I would like to do is to be able to override this...
2
by: Charles Law | last post by:
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...
7
by: fjlaga | last post by:
I have written an Office Add-in for Excel using VB.NET and the .NET 1.1 Framework (I have Visual Studio 2003 .NET ). All works great. I want to add a User Settings/Prefereneces dialog and allow...
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hi, I know this will sound like a lot of hand waving, and I'll be glad to supply some sample code if necessary, but I thought something obvious might jump out at someone without doing so. ...
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
8
by: swsdam | last post by:
Thank you for even reading this... I have an xml doc that feeds into html. I am being passed the xml via asp.net. I have used xsl to create an html. Now I'm being asked to let the user omit which...
0
by: dph5010 | last post by:
I'm trying to make an object that will reflect the response from isbndb.com's API so I can serialize the results into my code. Right now I have code that works, however, the structure isn't quite the...
0
by: =?Utf-8?B?RGF2ZSBU?= | last post by:
From a web service I'm serializing out an object model. Is there a way to only include those elements of the object model which actually contain data?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.