如何使用XMLSERIALIZE的枚举类型在C#中的属性?如何使用、属性、类型、XMLSERIALIZE

2023-09-03 05:53:25 作者:我姓全却不是你的全部メ

我有一个简单枚举:

enum simple 
{ 
  one, 
  two, 
  three 
};

我也有,有型简单的属性的类。我试图与属性装饰它: [XmlAttribute(数据类型=INT)] 。但是,当我尝试使用序列化失败的的XmlWriter

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?

推荐答案

根据达林季米特洛夫的答案 - 唯一的额外的事情,我想指出的是,如果你想在如何你的枚举字段的序列化失去控制,那么你可以装点每个现场的XmlEnum属性。

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