Raised when this XObject or any of its descendants have changed.
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
Events are raised only when an XML tree is modified, not when it is constructed. This is because you have to add an event handler to an event before you can receive events, and you cannot add an event handler before you have a reference to an XObject. You cannot 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 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.
This example produces the following output:
Changing event raised Sender: Child ObjectChange: Add Changed event raised Sender: Child ObjectChange: Add
Events are useful when you want to maintain some aggregate information in an XML tree. For example, you may want maintain an invoice total that is the sum of the line items of the invoice. This example uses events to maintain the total of all of the child elements under the complex element Items.
This code produces the following output:
Changed System.Xml.Linq.XElement Add Changed System.Xml.Linq.XElement Add Changed System.Xml.Linq.XText Remove Changed System.Xml.Linq.XText Add Changed System.Xml.Linq.XElement Add Changed System.Xml.Linq.XElement Remove Changed System.Xml.Linq.XElement Add Total:308 <Root> <Total>308</Total> <Items> <Item2>75</Item2> <Item3>133</Item3> <Item4>100</Item4> </Items> </Root>
.NET FrameworkSupported in: 4.5.2, 4.5.1, 4.5, 4, 3.5
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.