How do you use XMLSerialize for Enum typed properties in c#?

31,482

Solution 1

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,
}

Solution 2

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>
Share:
31,482
Rhubarb
Author by

Rhubarb

Updated on September 10, 2020

Comments

  • Rhubarb
    Rhubarb over 3 years

    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?

  • Rhubarb
    Rhubarb about 14 years
    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?
  • Darin Dimitrov
    Darin Dimitrov about 14 years
    Try decorating the property with XmlAttribute: [XmlAttribute("simple")]public Simple Simple { get; set; }
  • Rhubarb
    Rhubarb about 14 years
    Properties don't seem able to be decorated. Fields only, correct?
  • Anton
    Anton over 7 years
    Please, do not forget mark enum with [Serializable] attribute.
  • drzaus
    drzaus almost 6 years
    I might have a related problem - I used a generate-classes-from-schema tool (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
  • Ruturaj Patki
    Ruturaj Patki over 4 years
    @Anton, what if the class is already marked as Serializable? do we still need to mark the ENUM separately as Serializable?
  • Ruturaj Patki
    Ruturaj Patki over 4 years
    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.