XmlAttributes.XmlIgnore 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定一個值,指定是否 XmlSerializer 序列化公用欄位或公開讀寫屬性。
public:
property bool XmlIgnore { bool get(); void set(bool value); };
public bool XmlIgnore { get; set; }
member this.XmlIgnore : bool with get, set
Public Property XmlIgnore As Boolean
屬性值
true 若 不可 XmlSerializer 序列化欄位或屬性;否則, false。
範例
以下範例序列化一個名為 Group的類別,該類別包含一個名為 Comment 的成員,該 XmlIgnoreAttribute 成員被套用該類別。 範例中建立一個 XmlAttributes 物件,並將屬性 XmlIgnore 設為 false,從而覆蓋 XmlIgnoreAttribute。
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class Group
{
// The GroupName value will be serialized--unless it's overridden.
public string GroupName;
/* This field will be ignored when serialized--
unless it's overridden. */
[XmlIgnoreAttribute]
public string Comment;
}
public class Test
{
public static void Main()
{
Test t = new Test();
t.SerializeObject("IgnoreXml.xml");
}
// Return an XmlSerializer used for overriding.
public XmlSerializer CreateOverrider()
{
// Create the XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
/* Setting XmlIgnore to false overrides the XmlIgnoreAttribute
applied to the Comment field. Thus it will be serialized.*/
attrs.XmlIgnore = false;
xOver.Add(typeof(Group), "Comment", attrs);
/* Use the XmlIgnore to instruct the XmlSerializer to ignore
the GroupName instead. */
attrs = new XmlAttributes();
attrs.XmlIgnore = true;
xOver.Add(typeof(Group), "GroupName", attrs);
XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
return xSer;
}
public void SerializeObject(string filename)
{
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object to serialize and set its properties.
Group myGroup = new Group();
myGroup.GroupName = ".NET";
myGroup.Comment = "My Comment...";
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Serialize the object and close the TextWriter.
xSer.Serialize(writer, myGroup);
writer.Close();
}
}
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class Group
' The GroupName value will be serialized--unless it's overridden.
Public GroupName As String
' This field will be ignored when serialized--
' unless it's overridden.
<XmlIgnoreAttribute()> Public Comment As String
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
t.SerializeObject("IgnoreXml.xml")
End Sub
' Return an XmlSerializer used for overriding.
Public Function CreateOverrider() As XmlSerializer
' Create the XmlAttributeOverrides and XmlAttributes objects.
Dim xOver As New XmlAttributeOverrides()
Dim attrs As New XmlAttributes()
' Setting XmlIgnore to false overrides the XmlIgnoreAttribute
' applied to the Comment field. Thus it will be serialized.
attrs.XmlIgnore = False
xOver.Add(GetType(Group), "Comment", attrs)
' Use the XmlIgnore to instruct the XmlSerializer to ignore
' the GroupName instead.
attrs = New XmlAttributes()
attrs.XmlIgnore = True
xOver.Add(GetType(Group), "GroupName", attrs)
Dim xSer As New XmlSerializer(GetType(Group), xOver)
Return xSer
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create an XmlSerializer instance.
Dim xSer As XmlSerializer = CreateOverrider()
' Create the object to serialize and set its properties.
Dim myGroup As New Group()
myGroup.GroupName = ".NET"
myGroup.Comment = "My Comment..."
' Writing the file requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Serialize the object and close the TextWriter.
xSer.Serialize(writer, myGroup)
writer.Close()
End Sub
End Class
備註
預設情況下,所有 public 欄位與 public 讀寫屬性皆由 序列化。XmlSerializer 也就是說,每個公共欄位或屬性的值會以 XML 元素或 XML 屬性的形式在 XML 文件實例中持久化。
若要覆寫欄位或屬性的預設序列化,請建立一個 XmlAttributes 物件,並將其屬性設 XmlIgnore 為 true。
Add 物件的 XmlAttributeOverrides 物件,並指定包含要忽略欄位或屬性的物件型別,以及要忽略的欄位或屬性名稱。
若 a XmlIgnoreAttribute 應用於欄位或屬性,該欄位或屬性會被忽略。 不過你可以透過建立 XmlAttributes 一個物件,將其屬性設 XmlIgnore 為 false,加入 XmlAttributeOverrides 一個包含該欄位或屬性的物件類型,以及欄位或屬性名稱來覆蓋這個行為。