Export (0) Print
Expand All

XObjectChange Enumeration

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

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

public enum XObjectChange

Member nameDescription
Supported by the XNA FrameworkAddAn XObject has been or will be added to an XContainer.
Supported by the XNA FrameworkRemoveAn XObject has been or will be removed from an XContainer.
Supported by the XNA FrameworkNameAn XObject has been or will be renamed.
Supported by the XNA FrameworkValueThe 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, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft