This documentation is archived and is not being maintained.

Event Handling in an XML Document Using the XmlNodeChangedEventArgs

The XmlNodeChangedEventArgs encapsulates the arguments passed to the event handlers registered on the XmlDocument object for handling events. The events and a description of when they are fired is given in the following table.




When a node belonging to the current document is about to be inserted into another node.


When a node belonging to the current document has been inserted into another node.


When a node belonging to this document is about to be removed from the document.


When a node belonging to this document has been removed from its parent.


When the value of a node is about to be changed.


When the value of a node has been changed.


If the XmlDataDocument memory usage is fully optimized to use DataSet storage, the XmlDataDocument might not raise any of the events listed above when changes are made to the underlying DataSet. If you need these events, you must traverse the whole XmlDocument once to make the memory usage non-fully optimized.

The following code example shows how to define an event handler and how to add the event handler to an event.

// Attach the event handler, NodeInsertedHandler, to the NodeInserted
// event.
XmlDocument doc = new XmlDocument();
doc.NodeInserted += new XmlNodeChangedEventHandler(NodeInsertedHandler);
XmlNode n = doc.CreateElement( "element" );
Console.WriteLine( "Before Event Inserting" );

// This is the point where the new node is being inserted in the tree,
// and this is the point where the NodeInserted event is raised.
doc.AppendChild( n );
Console.WriteLine( "After Event Inserting" ); 

// Define the event handler that handles the NodeInserted event.
void NodeInsertedHandler(Object src, XmlNodeChangedEventArgs args)
    Console.WriteLine("Node " + args.Node.Name + " inserted!!");

Some XML Document Object Model (DOM) operations are compound operations that can result in multiple events being fired. For example, AppendChild may also have to remove the node being appended from its previous parent. In this case, you see a NodeRemoved event fired first, followed by a NodeInserted event. Operations like setting InnerXml could result in multiple events.

The following code example shows the creation of the event handler and the handling of the NodeInserted event.

using System;
using System.IO;
using System.Xml;

public class Sample
  private const String filename = "books.xml";

  public static void Main()
     Sample mySample = new Sample();

  public void Run(String args)
     // Create and load the XML document.
     Console.WriteLine ("Loading file {0} ...", args);
     XmlDocument doc = new XmlDocument();
     doc.Load (args);

     // Create the event handlers.
     doc.NodeChanged += new XmlNodeChangedEventHandler(this.MyNodeChangedEvent);
     doc.NodeInserted += new XmlNodeChangedEventHandler(this.MyNodeInsertedEvent);

     // Change the book price.
     doc.DocumentElement.LastChild.InnerText = "5.95";

     // Add a new element.
     XmlElement newElem = doc.CreateElement("style");
     newElem.InnerText = "hardcover";

     Console.WriteLine("\r\nDisplay the modified XML...");


  // Handle the NodeChanged event.
  public void MyNodeChangedEvent(Object src, XmlNodeChangedEventArgs args)
     Console.Write("Node Changed Event: <{0}> changed", args.Node.Name);
     if (args.Node.Value != null)
        Console.WriteLine(" with value  {0}", args.Node.Value);

  // Handle the NodeInserted event.
  public void MyNodeInsertedEvent(Object src, XmlNodeChangedEventArgs args)
     Console.Write("Node Inserted Event: <{0}> inserted", args.Node.Name);
     if (args.Node.Value != null)
        Console.WriteLine(" with value {0}", args.Node.Value);

} // End class 

For more information, see XmlNodeChangeEventArgs Members and XmlNodeChangedEventHandler Delegate.