XmlDocument Event: node changed, node inserted and node removed : XmlDocument « XML « C# / CSharp Tutorial






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);
    }       
  }
}
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








30.8.XmlDocument
30.8.1.XmlDocument Event: node changed, node inserted and node removed
30.8.2.Add attribute to Xml document
30.8.3.Read Write Xml
30.8.4.Remove child from XmlDocument
30.8.5.Get child count
30.8.6.Get element by tag name
30.8.7.Get value from selected node
30.8.8.Loop Through XmlDocument Recursively
30.8.9.Select Root
30.8.10.Three ways to load Xml to XmlDocument
30.8.11.Fast select by ID using DOM API
30.8.12.Create Node and append to XmlDocument
30.8.13.Select nodes from xml document