XObjectChange 列舉

定義

引發 XObject 的事件時,指定事件型別。Specifies the event type when an event is raised for an XObject.

public enum class XObjectChange
public enum XObjectChange
type XObjectChange = 
Public Enum XObjectChange
繼承
XObjectChange

欄位

Add 0

XObject 已經加入或將要加入 XContainerAn XObject has been or will be added to an XContainer.

Name 2

XObject 已經重新命名或將要重新命名。An XObject has been or will be renamed.

Remove 1

XObject 已經從 XContainer 中移除或將要移除。An XObject has been or will be removed from an XContainer.

Value 3

XObject 的值已經變更或將要變更。The value of an XObject has been or will be changed. 此外,空項目的序列化變更 (由空標記變更為開頭/結尾標記對,反之亦然) 會觸發此事件。In addition, a change in the serialization of an empty element (either from an empty tag to start/end tag pair or vice versa) raises this event.

範例

下列範例會藉由將元素新增至樹狀結構來引發事件。The following example raises an event by adding an element to the tree.

XElement root = new XElement("Root", "content");  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.Add(new XElement("Child", "child content"));  

這個範例會產生下列輸出:This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XElement  
  Changing: Add  
Changed event raised  
  Sender: System.Xml.Linq.XElement  
  Changed: Add  

下列範例會藉由從樹狀結構中移除元素來引發事件。The following example raises an event by removing an element from the tree.

XElement root = new XElement("Root",  
    new XElement("Child", "content")  
);  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.Element("Child").Remove();  

這個範例會產生下列輸出:This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XElement  
  Changing: Remove  
Changed event raised  
  Sender: System.Xml.Linq.XElement  
  Changed: Remove  

下列範例會藉由變更元素的名稱來引發事件。The following example raises an event by changing the name of an element.

XElement root = new XElement("Root", "content");  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.Name = "NewName";  

這個範例會產生下列輸出:This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XElement  
  Changing: Name  
Changed event raised  
  Sender: System.Xml.Linq.XElement  
  Changed: Name  

下列範例會藉由設定屬性的值來引發事件。The following example raises an event by setting the value of an attribute.

XElement root = new XElement("Root",  
    new XAttribute("Att", "att value")  
);  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.FirstAttribute.Value = "new contents";  

這個範例會產生下列輸出:This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XAttribute  
  Changing: Value  
Changed event raised  
  Sender: System.Xml.Linq.XAttribute  
  Changed: Value  

備註

這個 enum 會指定 XObject引發事件時的事件種類。This enum specifies the event type when an event is raised for an XObject.

修改 XML 樹狀結構的所有作業會細分成一系列的基本類型。All operations that modify the XML tree break down to a series of primitives. 基本類型共有四種。There are four types of primitives. 其中兩個基本專案(新增和移除)對集合採取動作。Two of the primitives (Add and Remove) act on collections. 其中兩個(名稱和值)在實例上作用。Two of them (Name and Value) act on instances. 每一個基本專案都有一個對應的事件。There is a corresponding event for each of these primitives.

修改其中一個事件中的 XML 樹狀結構時,請務必小心,因為這麼做可能會導致非預期的結果。You should be careful when modifying an XML tree within one of these events, because doing this might lead to unexpected results. 例如,如果您收到 Changing 事件,而當處理事件時,您會從樹狀結構中移除該節點,您可能不會收到 Changed 事件。For example, if you receive a Changing event, and while the event is being processed you remove the node from the tree, you might not receive the Changed event. 當處理事件時,修改包含接收事件之節點以外的 XML 樹狀結構是有效的。修改相同的樹狀結構甚至是有效的,因為修改並不會影響引發事件的特定節點。When an event is being processed, it is valid to modify an XML tree other than the one that contains the node that is receiving the event; it is even valid to modify the same tree provided the modifications do not affect the specific nodes on which the event was raised. 不過,如果您修改樹狀結構的區域,其中包含接收事件的節點,則您收到的事件和對樹狀結構的影響都是未定義的。However, if you modify the area of the tree that contains the node receiving the event, the events that you receive and the impact to the tree are undefined.

適用於

另請參閱