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
public 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
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlIgnoreAttribute = class
    inherit Attribute
Public Class XmlIgnoreAttribute
Inherits Attribute
繼承
XmlIgnoreAttribute
屬性

範例

下列範例顯示名為的類別 Group ,其中包含名為的欄位 CommentThe 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.

您可以藉 XmlIgnoreAttribute 由建立 XmlAttributes 物件,並將其屬性設定為,來覆寫所造成的行為 XmlIgnore falseYou can override the behavior caused by the XmlIgnoreAttribute by creating an XmlAttributes object, and setting its XmlIgnore property to false. 您必須 AddXmlAttributes 物件設為類別的實例 XmlAttributeOverridesYou must Add the XmlAttributes object to an instance of the XmlAttributeOverrides class. 最後,在 XmlAttributeOverrides XmlSerializer 呼叫或方法之前,您必須先使用物件來建立類別的實例 Serialize DeserializeLastly, you must use the XmlAttributeOverrides object to construct an instance of the XmlSerializer class before you call the Serialize or Deserialize methods.

( # A0) 的 XML 架構定義工具,偶爾會 XmlIgnoreAttribute 在從架構檔案 ( .xsd) 建立類別時產生。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 程式碼中的文字,而不是較長的 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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)

適用於

另請參閱