Partager via


XObject.Changed Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Raised when this XObject or any of its descendants have changed.

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

Syntax

'Declaration
Public Event Changed As EventHandler(Of XObjectChangeEventArgs)
public event EventHandler<XObjectChangeEventArgs> Changed

Remarks

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 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.

Examples

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.

StringBuilder output = new StringBuilder();
XElement root = new XElement("Root", "content");
root.Changing += new EventHandler<XObjectChangeEventArgs>(
    (sender, cea) =>
    {
        output.Append("Changing event raised" + Environment.NewLine);
        XElement xSender = (XElement)sender;
        output.Append("  Sender: " + xSender.Name + Environment.NewLine);
        output.Append("  ObjectChange: " + cea.ObjectChange + Environment.NewLine);
    }
);
root.Changed += new EventHandler<XObjectChangeEventArgs>(
    (sender, cea) =>
    {
        output.Append("Changed event raised" + Environment.NewLine);
        XElement xSender = (XElement)sender;
        output.Append("  Sender: " + xSender.Name + Environment.NewLine);
        output.Append("  ObjectChange: " + cea.ObjectChange + Environment.NewLine);
    }
);
root.Add(new XElement("Child", "child content"));

OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.