XObject.Changing 事件

定義

當此 XObject 或其任何子代發生變更時會引發。Raised when this XObject or any of its descendants are about to change.

public:
 event EventHandler<System::Xml::Linq::XObjectChangeEventArgs ^> ^ Changing;
public event EventHandler<System.Xml.Linq.XObjectChangeEventArgs> Changing;
member this.Changing : EventHandler<System.Xml.Linq.XObjectChangeEventArgs> 
Public Custom Event Changing As EventHandler(Of XObjectChangeEventArgs) 

範例

下列範例會將事件處理常式加入至 XML 樹狀結構的根項目。The following example adds an event handler to the root element of an XML tree. 然後,它會修改樹狀結構,導致 LINQ to XML 引發一些事件。It then modifies the tree, causing LINQ to XML to raise some events.

XElement root = new XElement("Root", "content");  
root.Changing += new EventHandler<XObjectChangeEventArgs>(  
    (sender, cea) =>  
    {  
        Console.WriteLine("Changing event raised");  
        XElement xSender = (XElement)sender;  
        Console.WriteLine("  Sender: {0}", xSender.Name);  
        Console.WriteLine("  ObjectChange: {0}", cea.ObjectChange);  
    }  
);  
root.Changed += new EventHandler<XObjectChangeEventArgs>(  
    (sender, cea) =>  
    {  
        Console.WriteLine("Changed event raised");  
        XElement xSender = (XElement)sender;  
        Console.WriteLine("  Sender: {0}", xSender.Name);  
        Console.WriteLine("  ObjectChange: {0}", cea.ObjectChange);  
    }  
);  
root.Add(new XElement("Child", "child content"));  
Module Module1  
    WithEvents root As XElement = <Root>content</Root>  
  
    Sub Main()  
        root.Add(<Child>child content</Child>)  
    End Sub  
  
    Private Sub root_Changing( _  
            ByVal sender As Object, _  
            ByVal e As XObjectChangeEventArgs) _  
            Handles root.Changing  
        Dim xSender As XElement = CType(sender, XElement)  
        Console.WriteLine("Changing event raised")  
        Console.WriteLine("  Sender: {0}", xSender.Name)  
        Console.WriteLine("  ObjectChange: {0}", e.ObjectChange)  
    End Sub  
  
    Private Sub root_Changed( _  
            ByVal sender As Object, _  
            ByVal e As XObjectChangeEventArgs) _  
            Handles root.Changed  
        Dim xSender As XElement = CType(sender, XElement)  
        Console.WriteLine("Changed event raised")  
        Console.WriteLine("  Sender: {0}", xSender.Name)  
        Console.WriteLine("  ObjectChange: {0}", e.ObjectChange)  
    End Sub  
End Module  

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

Changing event raised  
  Sender: Child  
  ObjectChange: Add  
Changed event raised  
  Sender: Child  
  ObjectChange: Add  

備註

事件只會從修改 XML 樹狀結構(而不是從 XML 樹狀結構的架構)中引發。Events are raised only from modification of an XML tree, not from construction of an XML tree. 您必須先將事件處理常式新增至事件,才可以接收事件,而且您必須先加入事件處理常式,才能參考XObjectYou have to add an event handler to an event before you can receive events, and you can't add an event handler before you have a reference to an XObject. 在建立 XML 樹狀結構XObject之前,您無法取得的參考。You can't get a reference to an XObject before the XML tree is constructed. 這表示在 XML 樹狀結構的功能結構中,您不會收到事件。This means that during functional construction of an XML tree, you will not receive events.

修改其中一個事件中的 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.

適用於

另請參閱