473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

show/hide the XML Serialization of a public property of enumerated type

In Summary:
Create an object - dont set one of its Enum Type properties and make
sure it does not appear in XML Object Serialization.

Strings function as hoped for but Enums do not

Heres my problem broken down to simplest form

I have the following ENUM
----------------------------------------------
Public Enum SexType
Male
Female
Unknown
End Enum
----------------------------------------------

& The Following Class
----------------------------------------------
Imports System.Xml.Serialization
<XmlRoot(ElementName:="Person", IsNullable:=False, [Namespace]:="")> _
Public Class Person

Private _name As String
Private _sex As SexType

<XmlElementAttribute(ElementName:="Sex",IsNullable :=False)> _
Public Property Sex As SexType
Get
Return _sex
End Get
Set(ByVal Value As SexType)
_sex = Value
End Set
End Property

<XmlElementAttribute("Name"),IsNullable:=False)> _
Public Property Name As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Sub New()
End Sub
-----------------------------------------------------


Now heres what i want to do in a Main method of the Console application
----------------------------------------------
Dim serializeAPP As XmlSerializer
Try
serializeAPP = New XmlSerializer(GetType(Person))
Catch e As Exception
Console.Write(e.Message())
Console.Read()
Exit Sub
End Try

Dim objPerson As New Person

objPerson.Name = "Graham"
serializeAPP.Serialize(Console.Out, objbControl)
Console.Read()
---------------------------------

The output is as follows (i've ommitted the namespace stuff)
---------------------------------
<Person>
<Name>Graham</Name>
<Sex>Male</Sex>
</Person>
---------------------------------
Thing is, you will note we did not set the Sex property. I want
properties that are not set not to appear in the output XML.

However if we do not the set the Name Property (which is of Type
String) you would note that it does not appear in the output XML.

i.e.
----------------------------------
<Person>
<Sex>Male</Sex>
</Person>
----------------------------------
I have set my XML attribute parameter IsNullable:=False

So In Summary:
How to make Unset Enum Type properties not appear in XML Serialization.




*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
0 1486

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alberto Grosso Nicolin | last post by:
We have the following XML schema: there's a root element (Response) with of a single child element (Result). ...
1
by: Opa | last post by:
Hi I'm having problem serialization an object instance which contains a public property on the object type My object hierarchy is many levels deep, so for simplicty I created to following which...
10
by: Michael Maes | last post by:
Hi, I have a BaseClass from which many Classes Derive. In short: the BaseClass provides the functionalities (Methods) and the Derived Classes extend it with Properties. One of the (Base)...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.