XmlIgnoreAttribute XmlIgnoreAttribute XmlIgnoreAttribute XmlIgnoreAttribute Class

定义

指示 Serialize(TextWriter, Object)XmlSerializer 方法不序列化公共字段或公共读/写属性值。Instructs the Serialize(TextWriter, Object) method of the XmlSerializer not to serialize the public field or public read/write property value.

public ref class XmlIgnoreAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlIgnoreAttribute : Attribute
type XmlIgnoreAttribute = class
    inherit Attribute
Public Class XmlIgnoreAttribute
Inherits Attribute
继承
XmlIgnoreAttributeXmlIgnoreAttributeXmlIgnoreAttributeXmlIgnoreAttribute
属性

示例

下面的示例演示一个名为Group的类,该类包含一个Comment名为的字段。The following example shows a class named Group, which contains a field named Comment. 该示例将分配XmlIgnoreAttribute给字段,从而指示在XmlSerializer序列化或反序列化类的实例时忽略字段。The example assigns the XmlIgnoreAttribute to the field, thereby instructing the XmlSerializer to ignore the field when serializing or deserializing an instance of the class.

public ref class Group
{
public:

   // The XmlSerializer ignores this field.

   [XmlIgnore]
   String^ Comment;

   // The XmlSerializer serializes this field.
   String^ GroupName;
};

public class Group
{
   // The XmlSerializer ignores this field.
   [XmlIgnore]
   public string Comment;

   // The XmlSerializer serializes this field.
   public string GroupName;
}
   
Public Class Group
    ' the XmlSerializer ignores this field.
    <XmlIgnore()> Public Comment As String
    
    ' The XmlSerializer serializes this field.
    Public GroupName As String
End Class

注解

属于一系列属性,这些属性控制XmlSerializer序列化或反序列化对象的方式。 XmlIgnoreAttributeThe XmlIgnoreAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. 如果将应用XmlIgnoreAttribute于类的任何成员,则XmlSerializer会在序列化或反序列化类的实例时忽略成员。If you apply the XmlIgnoreAttribute to any member of a class, the XmlSerializer ignores the member when serializing or deserializing an instance of the class. 有关类似属性的完整列表,请参阅控制 XML 序列化的属性For a complete list of similar attributes, see Attributes That Control XML Serialization.

通过XmlIgnoreAttribute XmlIgnore创建对象并将其属性设置为false,可以重写导致的行为。 XmlAttributesYou can override the behavior caused by the XmlIgnoreAttribute by creating an XmlAttributes object, and setting its XmlIgnore property to false. 您必须Add 具有类XmlAttributeOverrides的实例的对象。XmlAttributesYou must Add the XmlAttributes object to an instance of the XmlAttributeOverrides class. 最后, XmlAttributeOverrides在调用SerializeXmlSerializer 方法之前,必须使用对象来构造类的实例。DeserializeLastly, you must use the XmlAttributeOverrides object to construct an instance of the XmlSerializer class before you call the Serialize or Deserialize methods.

从架构文件(.xsd)创建类XmlIgnoreAttribute时, XML 架构定义工具(xsd.exe)有时会生成。The XML Schema Definition Tool (Xsd.exe) occasionally generates the XmlIgnoreAttribute when creating classes from a schema file (.xsd). 出现此行为是因为值类型不能设置null为,但所有 XML 数据类型都可以是。This behavior occurs because value types cannot be set to null, but all XML data types can be. 因此,该工具会在遇到映射到值类型的 XML 类型时创建两个字段:一个字段用于保存值,另一个采用的形式fieldnameSpecified为,其中的fieldname被替换为字段或属性的名称。Therefore, the tool creates two fields when it encounters an XML type that maps to a value type: one to hold the value and another special field that takes the form of fieldnameSpecified, where the fieldname is replaced by the name of the field or property. 但请注意,只有当架构指定元素没有最小出现次数(minOccurs = "0"),并且该元素没有默认值时,才会生成此特殊字段。Notice, however, that this special field is generated only when the schema specifies that the element has no minimum occurrence (minOccurs = "0") and that the element has no default value. XmlSerializer设置并检查此特殊字段,以确定是否已为该字段或属性设置值。The XmlSerializer sets and checks this special field to determine whether a value has been set for the field or property. 由于特殊字段不能进行序列化,因此该工具将XmlIgnoreAttribute应用于该字段。Because the special field must not be serialized, the tool applies the XmlIgnoreAttribute to it.

有关使用特性的详细信息,请参阅特性For more information about using attributes, see Attributes.

备注

您可以在代码中XmlIgnore使用该单词而不是更XmlIgnoreAttribute长的时间。You can use the word XmlIgnore in your code instead of the longer XmlIgnoreAttribute.

构造函数

XmlIgnoreAttribute() XmlIgnoreAttribute() XmlIgnoreAttribute() XmlIgnoreAttribute()

初始化 XmlIgnoreAttribute 类的新实例。Initializes a new instance of the XmlIgnoreAttribute class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

属性

TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

适用于

另请参阅