XmlIgnoreAttribute 類別

定義

表示 Serialize(TextWriter, Object)XmlSerializer 方法不要序列化公用欄位或公用讀取/寫入屬性值。

public ref class XmlIgnoreAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlIgnoreAttribute : 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 的類別,其中包含名為 的 Comment 欄位。 此範例會將 XmlIgnoreAttribute 指派給 欄位,藉此指示 XmlSerializer 在序列化或還原序列化 類別的實例時忽略 欄位。

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 。 如果您將 套用 XmlIgnoreAttribute 至類別的任何成員,則會 XmlSerializer 在序列化或還原序列化 類別的實例時忽略成員。 如需類似屬性的完整清單,請參閱 控制 XML 序列化的屬性

您可以藉由建立 XmlAttributes 物件,並將其 屬性設定 XmlIgnorefalse ,來覆寫 所造成的 XmlIgnoreAttribute 行為。 您必須 AddXmlAttributes 物件設為 類別的 XmlAttributeOverrides 實例。 最後,您必須先使用 XmlAttributeOverrides 物件來建構 類別的 XmlSerializer 實例,才能呼叫 SerializeDeserialize 方法。

XML 架構定義工具 (Xsd.exe) 偶爾會在從架構檔案 (.xsd) 建立類別時產生 XmlIgnoreAttribute 。 發生此行為的原因是實值型別無法設定為 null ,但所有 XML 資料類型都可以。 因此,當工具遇到對應至實數值型別的 XML 類型時,會建立兩個欄位:一個用來保存值,另一個採用 格式的特殊欄位 fieldnameSpecified ,其中 fieldname 會以欄位或屬性的名稱取代 。 不過請注意,只有當架構指定元素沒有最小出現次數時,才會產生這個特殊欄位, (minOccurs = 「0」) ,而且元素沒有預設值。 會 XmlSerializer 設定並檢查這個特殊欄位,以判斷欄位或屬性是否已設定值。 因為特殊欄位不得序列化,所以此工具會將 套 XmlIgnoreAttribute 用至它。

如需使用屬性的詳細資訊,請參閱 屬性

注意

您可以在程式碼中使用字組 XmlIgnore ,而不是較長 XmlIgnoreAttribute 的 。

建構函式

XmlIgnoreAttribute()

初始化 XmlIgnoreAttribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱