using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
public class MainClass{
public static void Main() {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<Record> Some Value </Record>");
//Create the event handlers.
xmlDoc.NodeChanged += new XmlNodeChangedEventHandler(MyNodeChangedEvent);
xmlDoc.NodeInserted +=new XmlNodeChangedEventHandler(MyNodeInsertedEvent);
xmlDoc.NodeRemoved += new XmlNodeChangedEventHandler(MyNodeRemovedEvent);
XmlElement root = xmlDoc.DocumentElement;
string str = root.ToString();
XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();
xmlDocFragment.InnerXml="<F><S>Data</S></F>";
XmlElement rootNode = xmlDoc.DocumentElement;
rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild);
XmlNode node = xmlDoc.LastChild;
xmlDoc.RemoveChild(node);
}
public static void MyNodeChangedEvent(Object src, XmlNodeChangedEventArgs args)
{
Console.WriteLine("Node Changed Event Fired for node "+ args.Node.Name);
if (args.Node.Value != null)
{
Console.WriteLine(args.Node.Value);
}
}
public static void MyNodeInsertedEvent(Object src, XmlNodeChangedEventArgs args)
{
Console.WriteLine("Node Inserted Event Fired for node "+ args.Node.Name);
if (args.Node.Value != null)
{
Console.WriteLine(args.Node.Value);
}
}
public static void MyNodeRemovedEvent(Object src, XmlNodeChangedEventArgs args){
Console.WriteLine("Node Removed Event Fired for node "+ args.Node.Name);
if (args.Node.Value != null)
{
Console.WriteLine(args.Node.Value);
}
}
}
Output
Node Inserted Event Fired for node S
Node Inserted Event Fired for node #text
Data
Node Inserted Event Fired for node F
Node Removed Event Fired for node #text
Some Value
Node Removed Event Fired for node F
Node Inserted Event Fired for node F
Node Removed Event Fired for node Record