Working with XmlDataDocument Events in ADO.NET

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

The XmlDataDocument events are useful when your application needs to notify you when changes are being made to an XmlDataDocument object. XmlDocument defines XmlDataDocument events (see Table 9-6).

Table 9-6. XmlDataDocument Events




Occurs when the value of a node has been changed


Occurs when the value of a node is changing


Occurs when a node inserted into another node


Occurs when a node inserting to another node


Occurs when a node has been removed


Occurs when a node is being removed

The XmlNodeChangedEventHandler method handles the events listed in Table 9-6. The XmlNodeChangedEventHandler is as follows:

        public delegate void XmlNodeChangedEventHandler(object sender, XmlNodeChangedEventArgs e);

Where sender is the source of the event and e is an XmlNodeChangedEventArgs that contains the event data. XmlNodeChangedEventArgs defines properties (see Table 9-7).

Table 9-7. The XmlNodeChangedEventArgs properties




Returns a value indicating the type of node changed event


Returns a value of parent node after the operation is finished


Returns the node that is being added, removed, or changed


Returns the value of the parent node before operation started

Listing 9-11. Handles XmlDataDocument events. The XmlDocumentBtn_Click method creates event handlers for the NodeChanged, NodeInserted, and NodeRemoved events. The MyNodeChangedEvent, MyNodeInsertEvent, and MyNodeRemoved event handlers execute when these events fire. I used LoadXml to load an XML fragment and then used the ReplaceChild and RemoveChild methods to replace and remove document nodes.

Listing 9-11. The XmlDataDocument event handling sample

        private void XmlDocumentBtn_Click(object sender, System.EventArgs e)
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("<Record> Some Value </Record>");

             // Create the event handlers.
            xmlDoc.NodeChanged +=
            new XmlNodeChangedEventHandler(this.MyNodeChangedEvent);
             xmlDoc.NodeInserted +=
            new XmlNodeChangedEventHandler(this.MyNodeInsertedEvent);
            xmlDoc.NodeRemoved +=
            new XmlNodeChangedEventHandler(this.MyNodeRemovedEvent);
            XmlElement root = xmlDoc.DocumentElement;
            string str = root.ToString();
            XmlDocumentFragment xmlDocFragment =
             xmlDocFragment.InnerXml =
            "<Fragment><SomeDate>Fragment Data</SomeDate></Fragment>";

            // Replace Node
            XmlElement rootNode = xmlDoc.DocumentElement;
             rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild);

             //Remove Node
            XmlNode node = xmlDoc.LastChild;

Listing 9-12. Shows the NodeChangedEvent handler. The Node property of XmlNodeChangedEventArgs returns XmlNode. Using the Node property you can get more information about a node such as its parent node, value, name, namespace, and so on. 

Listing 9-12. The NodeChanged event handler

        public void MyNodeChangedEvent(object src,
        XmlNodeChangedEventArgs args)
            ("Node Changed Event Fired for node " + args.Node.Name);
            if (args.Node.Value != null)

Similar to listing 9-12, Listing 9-13 and 9-14 show event handlers for the NodeInserted and NodeRemoved events.

Listing 9-13. The NodeInserted event handler

        public void MyNodeInsertedEvent(object src,
        XmlNodeChangedEventArgs args)
            ("Node Inserted event fired for node " + args.Node.Name);

            if (args.Node.Value != null)

Listing 9-14.The NodeRemoved event handler

        public void MyNodeRemovedEvent(object src,
        XmlNodeChangedEventArgs args)
            ("Node Removed event fired for node " + args.Node.Name);
            if (args.Node.Value != null)


Hope this article would have helped you in understanding working with XmlDataDocument Events in ADO.NET. See other articles on the website also for further reference.

This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.

Up Next
    Ebook Download
    View all
    View all