XObjectChange XObjectChange XObjectChange XObjectChange Enum

Definition

Specifies the event type when an event is raised for an XObject.

public enum class XObjectChange
public enum XObjectChange
type XObjectChange = 
Public Enum XObjectChange
Inheritance
XObjectChangeXObjectChangeXObjectChangeXObjectChange

Fields

Add Add Add Add 0

An XObject has been or will be added to an XContainer.

Name Name Name Name 2

An XObject has been or will be renamed.

Remove Remove Remove Remove 1

An XObject has been or will be removed from an XContainer.

Value Value Value Value 3

The value of an XObject has been or will be changed. In addition, a change in the serialization of an empty element (either from an empty tag to start/end tag pair or vice versa) raises this event.

Examples

The following example raises an event by adding an element to the tree.

XElement root = new XElement("Root", "content");  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.Add(new XElement("Child", "child content"));  

This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XElement  
  Changing: Add  
Changed event raised  
  Sender: System.Xml.Linq.XElement  
  Changed: Add  

The following example raises an event by removing an element from the tree.

XElement root = new XElement("Root",  
    new XElement("Child", "content")  
);  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.Element("Child").Remove();  

This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XElement  
  Changing: Remove  
Changed event raised  
  Sender: System.Xml.Linq.XElement  
  Changed: Remove  

The following example raises an event by changing the name of an element.

XElement root = new XElement("Root", "content");  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.Name = "NewName";  

This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XElement  
  Changing: Name  
Changed event raised  
  Sender: System.Xml.Linq.XElement  
  Changed: Name  

The following example raises an event by setting the value of an attribute.

XElement root = new XElement("Root",  
    new XAttribute("Att", "att value")  
);  
root.Changing += (sender, e) =>  
    {  
        Console.WriteLine("Changing event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changing: " + e.ObjectChange);  
    };  
root.Changed += (sender, e) =>  
    {  
        Console.WriteLine("Changed event raised");  
        Console.WriteLine("  Sender: " + sender.GetType());  
        Console.WriteLine("  Changed: " + e.ObjectChange);  
    };  
root.FirstAttribute.Value = "new contents";  

This example produces the following output:

Changing event raised  
  Sender: System.Xml.Linq.XAttribute  
  Changing: Value  
Changed event raised  
  Sender: System.Xml.Linq.XAttribute  
  Changed: Value  

Remarks

This enum specifies the event type when an event is raised for an XObject.

All operations that modify the XML tree break down to a series of primitives. There are four types of primitives. Two of the primitives (Add and Remove) act on collections. Two of them (Name and Value) act on instances. There is a corresponding event for each of these primitives.

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.

Applies to

See also