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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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 ***
.



Relevant Pages

  • Re: Need XML Serialization Help (Long Post!)
    ... > learn) and XML serialization is all new ground for me. ... > Private Sub CreateXML(ByVal filename As String) ... > Public Class ShowBase ...
    (microsoft.public.dotnet.xml)
  • Re: ToString use with serialization vs other uses
    ... It's a standard way to provide custom serialization to/from XML. ... > Will using this interface require me to use the objects ToString method? ... >>> display an objects value as a human readable formated string. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Serializing a Dictionary As Xml Attributes
    ... go about implementing serialization that turns a string dictionary to ... ' I want the key/value pairs to be xml attributes of the root. ... Public FooAttributes As Dictionary(Of String, ... When I serialize an instance of Foo to XML I would like FooAttributes ...
    (microsoft.public.dotnet.xml)
  • WSDL2Java classes to XML
    ... Does anyone know how to go from a WSDL2Java generated class to an XML ... string of XML document. ... but I don't know what classes to call to do the serialization. ...
    (comp.lang.java)
  • Re: enums: What the H? again
    ... > find no sign of it in the Enum class until I did a decompile and saw ... > private String shortName; ... > private static InDir currentState = UNKNOWN; ...
    (comp.lang.java.programmer)