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

範例

下列範例顯示名為類別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物件的執行個體XmlAttributeOverrides類別。You must Add the XmlAttributes object to an instance of the XmlAttributeOverrides class. 最後,您必須使用XmlAttributeOverrides物件以建構的執行個體XmlSerializer類別,然後再呼叫SerializeDeserialize方法。Lastly, you must use the XmlAttributeOverrides object to construct an instance of the XmlSerializer class before you call the Serialize or Deserialize methods.

XML 結構描述定義工具 (Xsd.exe)偶爾會產生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.

注意

您可以使用 wordXmlIgnore代替較長的程式碼中XmlIgnoreAttributeYou 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 的淺層複本 (Shallow Copy)。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)

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

適用於

另請參閱