XmlIgnoreAttribute 类

定义

指示 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
继承
XmlIgnoreAttribute
属性

示例

下面的示例演示一个名为 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

注解

XmlIgnoreAttribute 属于控制 XmlSerializer 如何序列化或反序列化对象的一系列属性。The 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.

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

从架构文件(.xsd)创建类时, XML 架构定义工具(xsd.exe)有时会生成 XmlIgnoreAttributeThe 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. 由于特殊字段不能进行序列化,因此该工具向其应用 XmlIgnoreAttributeBecause the special field must not be serialized, the tool applies the XmlIgnoreAttribute to it.

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

备注

您可以在代码中使用 word XmlIgnore 而不是更长的 XmlIgnoreAttributeYou can use the word XmlIgnore in your code instead of the longer XmlIgnoreAttribute.

构造函数

XmlIgnoreAttribute()

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

属性

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()

获取当前实例的 TypeGets 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)

适用于

另请参阅