次の方法で共有


XObjectChange Enumeration

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

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)

Syntax

'Declaration
Public Enumeration XObjectChange
public enum XObjectChange

Members

Member name Description
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Add An XObject has been or will be added to an XContainer.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Remove An XObject has been or will be removed from an XContainer.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Name An XObject has been or will be renamed.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Value 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.

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.

Examples

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

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

OutputTextBlock.Text = output.ToString();

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

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

OutputTextBlock.Text = output.ToString();

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

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

OutputTextBlock.Text = output.ToString();

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

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

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.