Export (0) Print
Expand All

XObject.Changing Event

Raised when this XObject or any of its descendants are about to change.

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

'Declaration
Public Event Changing As EventHandler(Of XObjectChangeEventArgs)

Events are raised only from modification of an XML tree, not from construction of an XML tree. You 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. You can't get a reference to an XObject before the XML tree is constructed. This means that during functional construction of an XML tree, you will not receive events.

You should be careful when modifying an XML tree within one of these events, because doing this might lead to unexpected results. 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. 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.

The following example adds an event handler to the root element of an XML tree. It then modifies the tree, causing LINQ to XML to raise some events.

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

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft