Board index » Visual Studio » show/hide the XML Serialization of a public property of enumerated type
|
mmk
|
show/hide the XML Serialization of a public property of enumerated type
Visual Studio164
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 www.developersdex.com *** - |
