CSharp - LINQ XML XML Events

Introduction

We can register events to XML so that you can be notified any time any object inheriting from XObject is about to be, or has been, modified.

XObject.Changing

This event is raised when an object inheriting from XObject is about to be changed.

You register for the event by adding an object of type EventHandler to the object's Changing event like this:

myobject.Changing += new EventHandler<XObjectChangeEventArgs>(MyHandler);

where your method delegate must match this signature:

void MyHandler(object sender, XObjectChangeEventArgs cea)

The sender object is the object that is about to be changed, which is causing the event to be raised.

The change event arguments contain a property named ObjectChange of type XObjectChange indicating the type of change about to take place: XObjectChange.Add, XObjectChange.Name, XObjectChange.Remove, or XObjectChange.Value.

XObject.Changed

This event is raised after an object inheriting from XObject has been changed.

You register for the event by adding an object of type EventHandler to the object's Changed event like this:

myobject.Changed += new EventHandler<XObjectChangeEventArgs>(MyHandler);

where your method delegate must match this signature:

void MyHandler(object sender, XObjectChangeEventArgs cea)

The sender object is the object that has changed, which caused the event to be raised.

The change event arguments contain a property named ObjectChange of type XObjectChange indicating the type of change that has taken place: XObjectChange.Add, XObjectChange.Name, XObjectChange.Remove, or XObjectChange.Value.

Demo

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

class Program/*  www .  j av a2s  . c  om*/
{
    static void Main(string[] args)
    {
        XElement firstParticipant;

        XDocument xDocument = new XDocument(
          new XElement("Books", firstParticipant =
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "Joe"),
              new XElement("LastName", "Ruby")),
            new XElement("Book",
              new XAttribute("type", "Editor"),
              new XElement("FirstName", "PHP"),
              new XElement("LastName", "Python"))));

        Console.WriteLine("{0}{1}", xDocument, System.Environment.NewLine);

        firstParticipant.Changing += new EventHandler<XObjectChangeEventArgs>(
          (object sender, XObjectChangeEventArgs cea) =>
            Console.WriteLine("Type of object changing: {0}, Type of change: {1}",
              sender.GetType().Name, cea.ObjectChange));

        firstParticipant.Changed += (object sender, XObjectChangeEventArgs cea) =>
          Console.WriteLine("Type of object changed: {0}, Type of change: {1}",
            sender.GetType().Name, cea.ObjectChange);

        xDocument.Changed += (object sender, XObjectChangeEventArgs cea) =>
          Console.WriteLine("Doc: Type of object changed: {0}, Type of change: {1}{2}",
            sender.GetType().Name, cea.ObjectChange, System.Environment.NewLine);

      
        firstParticipant.Element("FirstName").Value = "Seph";

        Console.WriteLine("{0}{1}", xDocument, System.Environment.NewLine);
    }
}

Result