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當引發的事件時,這會指定事件種類 XObjectThis 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 樹狀結構,不是包含接收事件之節點的 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.

適用於