XObject.Changing Event
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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)
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.
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
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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: