Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
XObjectChange Enumeration

XObjectChange Enumeration

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

Namespace:  System.Xml.Linq
Assemblies:   System.Xml.XDocument (in System.Xml.XDocument.dll)
  System.Xml.Linq (in System.Xml.Linq.dll)

public enum XObjectChange

Member nameDescription
Supported by the XNA FrameworkSupported by Portable Class LibraryAddAn XObject has been or will be added to an XContainer.
Supported by the XNA FrameworkSupported by Portable Class LibraryNameAn XObject has been or will be renamed.
Supported by the XNA FrameworkSupported by Portable Class LibraryRemoveAn XObject has been or will be removed from an XContainer.
Supported by the XNA FrameworkSupported by Portable Class LibraryValueThe 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.

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.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft