Working with XmlDataDocument Events in ADO.NET

This article has been excerpted from the 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
 
EVENT DESCRIPTION
NodeChanged
Occurs when the value of a node has been changed
NodeChanging
Occurs when the value of a node is changing
NodeInserted
Occurs when a node inserted into another node
NodeInserting
Occurs when a node inserting to another node
NodeRemove
Occurs when a node has been removed
NodeRemoving
Occurs when a node is being removed
 
The XmlNodeChangedEventHandler method handles the events listed in Table 9-6. The XmlNodeChangedEventHandler is as follows:
  1. 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
 
PROPERTY DESCRIPTION
Action
Returns a value indicating the type of node changed event
NewParent
Returns a value of parent node after the operation is finished
Node
Returns the node that is being added, removed, or changed
OldParent
Returns the value of the parent node before the 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
  1. private void XmlDocumentBtn_Click(object sender, System.EventArgs e) {  
  2.     XmlDocument xmlDoc = new XmlDocument();  
  3.     xmlDoc.LoadXml("<Record> Some Value </Record>");  
  4.     // Create the event handlers.  
  5.     xmlDoc.NodeChanged +=  
  6.         new XmlNodeChangedEventHandler(this.MyNodeChangedEvent);  
  7.     xmlDoc.NodeInserted +=  
  8.         new XmlNodeChangedEventHandler(this.MyNodeInsertedEvent);  
  9.     xmlDoc.NodeRemoved +=  
  10.         new XmlNodeChangedEventHandler(this.MyNodeRemovedEvent);  
  11.     XmlElement root = xmlDoc.DocumentElement;  
  12.     string str = root.ToString();  
  13.     XmlDocumentFragment xmlDocFragment =  
  14.         xmlDoc.CreateDocumentFragment();  
  15.     xmlDocFragment.InnerXml =  
  16.         "<Fragment><SomeDate>Fragment Data</SomeDate></Fragment>";  
  17.     // Replace Node  
  18.     XmlElement rootNode = xmlDoc.DocumentElement;  
  19.     rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild);  
  20.     //Remove Node  
  21.     XmlNode node = xmlDoc.LastChild;  
  22.     xmlDoc.RemoveChild(node);  
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
  1. public void MyNodeChangedEvent(object src,  
  2.     XmlNodeChangedEventArgs args) {  
  3.     MessageBox.Show("Node Changed Event Fired for node " + args.Node.Name);  
  4.     if (args.Node.Value != null) {  
  5.         MessageBox.Show(args.Node.Value);  
  6.     }  
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
  1. public void MyNodeInsertedEvent(object src,  
  2.     XmlNodeChangedEventArgs args) {  
  3.     MessageBox.Show("Node Inserted event fired for node " + args.Node.Name);  
  4.     if (args.Node.Value != null) {  
  5.         MessageBox.Show(args.Node.Value);  
  6.     }  
Listing 9-14.The NodeRemoved event handler
  1. public void MyNodeRemovedEvent(object src,  
  2.     XmlNodeChangedEventArgs args) {  
  3.     MessageBox.Show("Node Removed event fired for node " + args.Node.Name);  
  4.     if (args.Node.Value != null) {  
  5.         MessageBox.Show(args.Node.Value);  
  6.     }  

Conclusion

 
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.
 
adobook.jpg
 
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.



Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.