如何使用XMLSERIALIZE的枚举类型在C#中的属性? [英] How do you use XMLSerialize for Enum typed properties in c#?

查看:979
本文介绍了如何使用XMLSERIALIZE的枚举类型在C#中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单枚举:

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?

推荐答案

根据达林季米特洛夫的答案 - 唯一的额外的事情,我想指出的是,如果你想在如何你的枚举字段的序列化失去控制,那么你可以装点每个现场的<一个href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlenumattribute.aspx">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,
}

这篇关于如何使用XMLSERIALIZE的枚举类型在C#中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆