How to serialize/deserialize optional XML enumeration in C#?

Highwind

New Member
I am trying to figure out how to serialize/deserialize an XML listing to C# that has an optional attribute that is an enumerated type. The following is my C# class:\[code\]public class AttributeAssignmentExpressionElement : XACMLElement{ [XmlAttribute] public string AttributeId { get; set; } [XmlAttribute] public Category Category { get; set; } }\[/code\]My \[code\]Category\[/code\] enumeration is defined as follows:\[code\]public enum Category{ [XmlEnum(Name = "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject")] Subject, [XmlEnum(Name = "urn:oasis:names:tc:xacml:3.0:attribute-category:resource")] Resource, [XmlEnum(Name = "urn:oasis:names:tc:xacml:3.0:attribute-category:action")] Action, [XmlEnum(Name = "urn:oasis:names:tc:xacml:3.0:attribute-category:environment")] Environment} \[/code\]When \[code\]Category\[/code\] is present in the corresponding XML file, serialization/deserialization works as expected. However if the \[code\]Category\[/code\] is missing from the XML, the default value is used (first item in the enumeration). If I try to make the enumerated variable nullable (\[code\]Category?\[/code\]), the deserializer throws an exception because it is unable to deserialize a complex type. Given the following XML (which does not contain the attribute), how can I serialize the enumeration appropriately?\[code\]<AttributeAssignmentExpression AttributeId="urn:oasis:names:tc:xacml:3.0:example:attribute:text"> </AttributeAssignmentExpression>\[/code\]In this situation, the value in the deserialized object should be null.Thanks for any help you can offer!
 
Back
Top