XmlEnumAttribute 类
定义
控制 XmlSerializer 如何序列化枚举成员。Controls how the XmlSerializer serializes an enumeration member.
public ref class XmlEnumAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field)]
public class XmlEnumAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field)>]
type XmlEnumAttribute = class
inherit Attribute
Public Class XmlEnumAttribute
Inherits Attribute
- 继承
- 属性
示例
下面的示例将应用于 XmlEnumAttribute 枚举的成员。The following example applies the XmlEnumAttribute to the members of an enumeration. 当为 XmlSerializer 此枚举生成 XML 数据时,数据符合属性的值 Name 。When the XmlSerializer generates XML data for this enumeration, the data conforms to the values of the Name properties.
public enum class EmployeeStatus
{
[XmlEnum(Name = "Single")]
One,
[XmlEnum(Name = "Double")]
Two,
[XmlEnum(Name = "Triple")]
Three
};
public enum EmployeeStatus
{
[XmlEnum(Name = "Single")]
One,
[XmlEnum(Name = "Double")]
Two,
[XmlEnum(Name = "Triple")]
Three
}
Public Enum EmployeeStatus
<XmlEnum("Single")> One = 1
<XmlEnum("Double")> Two = 2
<XmlEnum("Triple")> Three = 3
End Enum
备注
您可以 XmlEnum 在代码中使用该单词而不是更长的时间 XmlEnumAttribute 。You can use the word XmlEnum in your code instead of the longer XmlEnumAttribute.
注解
XmlEnumAttribute属于一系列属性,这些属性控制 XmlSerializer 序列化或反序列化对象的方式。The XmlEnumAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. 有关类似属性的完整列表,请参阅 控制 XML 序列化的属性。For a complete list of similar attributes, see Attributes That Control XML Serialization.
使用 XmlEnumAttribute 可以更改在 XmlSerializer 序列化或反序列化类时生成或识别 (的枚举,分别) 。Use the XmlEnumAttribute to change the enumeration that the XmlSerializer generates or recognizes (when it serializes or deserializes a class, respectively). 例如,如果枚举包含名为的成员 One ,但您更喜欢指定 XML 输出 Single ,请将应用于 XmlEnumAttribute 枚举成员,并将 Name 属性设置为 "Single"。For example, if an enumeration contains a member named One, but you prefer that the XML output be named Single, apply the XmlEnumAttribute to the enumeration member and set the Name property to "Single".
您可以 Name XmlEnumAttribute 通过创建类的实例 XmlEnumAttribute 并将其分配给 XmlEnum 对象的属性,来重写的属性值 XmlAttributes 。You can override the Name property value of an XmlEnumAttribute by creating an instance of the XmlEnumAttribute class and assigning it to the XmlEnum property of an XmlAttributes object. 有关详细信息,请参阅 XmlAttributeOverrides 类。For details, see the XmlAttributeOverrides class.
备注
您可以 XmlEnum 在代码中使用该单词而不是更长的时间 XmlEnumAttribute 。You can use the word XmlEnum in your code instead of the longer XmlEnumAttribute.
构造函数
| XmlEnumAttribute() |
初始化 XmlEnumAttribute 类的新实例。Initializes a new instance of the XmlEnumAttribute class. |
| XmlEnumAttribute(String) |
初始化 XmlEnumAttribute 类的新实例,并指定 XmlSerializer 生成或识别的(当该序列化程序分别序列化或反序列化枚举时)XML 值。Initializes a new instance of the XmlEnumAttribute class, and specifies the XML value that the XmlSerializer generates or recognizes (when it serializes or deserializes the enumeration, respectively). |
属性
| Name |
获取或设置当 XmlSerializer 序列化枚举时在 XML 文档实例中生成的值,或当它反序列化该枚举成员时识别的值。Gets or sets the value generated in an XML-document instance when the XmlSerializer serializes an enumeration, or the value recognized when it deserializes the enumeration member. |
| TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute. (继承自 Attribute) |
方法
| Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object. (继承自 Attribute) |
| GetHashCode() |
返回此实例的哈希代码。Returns the hash code for this instance. (继承自 Attribute) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (继承自 Attribute) |
| Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers. (继承自 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface. (继承自 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1). (继承自 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object. (继承自 Attribute) |
适用于
另请参阅
- Serialize(TextWriter, Object)
- Deserialize(Stream)
- XmlSerializer
- XmlEnum
- XmlAttributeOverrides
- XmlAttributes
- XML 序列化简介Introducing XML Serialization
- 如何:指定 XML 流的替代元素名称How to: Specify an Alternate Element Name for an XML Stream
- 使用属性控制 XML 序列化Controlling XML Serialization Using Attributes
- XML 序列化示例Examples of XML Serialization
- XML Schema Definition Tool (Xsd.exe)XML Schema Definition Tool (Xsd.exe)