30

I have a simple enum:

enum simple 
{ 
  one, 
  two, 
  three 
};

I also have a class that has a property of type simple. I tried decorating it with the attribute: [XmlAttribute(DataType = "int")]. However, it fails when I try to serialize it using an XmlWriter.

What is the proper way to do this? Do I have to mark the enum itself as well as the property, and if so, with which data type?

Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
Rhubarb
  • 3,893
  • 6
  • 41
  • 55

2 Answers2

53

As per Darin Dimitrov's answer - only extra thing I'd point out is that if you want control over how your enum fields are serialized out then you can decorate each field with the XmlEnum attribute.

public enum Simple
{
      [XmlEnum(Name="First")]
      one,
      [XmlEnum(Name="Second")]
      two,
      [XmlEnum(Name="Third")]
      three,
}
zebrabox
  • 5,694
  • 1
  • 28
  • 32
  • 4
    Please, do not forget mark enum with [Serializable] attribute. – Anton Aug 15 '16 at 10:52
  • @Anton, what if the class is already marked as Serializable? do we still need to mark the ENUM separately as Serializable? – Ruturaj Patki Dec 05 '19 at 10:55
  • I followed your method as well as the code in above; but I still get following error in VB.net code. ```Enum underlying type and the object must be same type or object. Type passed in was 'System.String'; the enum underlying type was 'System.Int32'.``` Nothing special in class... just a test class with one property and one enum. – Ruturaj Patki Dec 05 '19 at 11:00
24

There shouldn't be any problems serializing enum properties:

public enum Simple { one, two, three }

public class Foo
{
    public Simple Simple { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (var writer = XmlWriter.Create(Console.OpenStandardOutput()))
        {
            var foo = new Foo
            {
                Simple = Simple.three
            };
            var serializer = new XmlSerializer(foo.GetType());
            serializer.Serialize(writer, foo);
        }
    }
}

produces:

<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Simple>three</Simple>
</Foo>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    That works but it makes the enum property an element not an attribute. When I try to make it an attribute it fails. Any suggestions? – Rhubarb Feb 21 '10 at 20:15
  • 2
    Try decorating the property with XmlAttribute: `[XmlAttribute("simple")]public Simple Simple { get; set; }` – Darin Dimitrov Feb 21 '10 at 21:58
  • Properties don't seem able to be decorated. Fields only, correct? – Rhubarb Feb 24 '10 at 23:51
  • I might have a related problem - I used a [generate-classes-from-schema tool](https://github.com/mganss/XmlSchemaClassGenerator) (not `xsd`) which may have over- (under?) attributed the properties, because in my case the enum properties aren't being included in the serialization. They – drzaus May 25 '18 at 19:16