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:
- 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
- private void XmlDocumentBtn_Click(object sender, System.EventArgs e) {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml("<Record> Some Value </Record>");
-
- 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 =
- xmlDoc.CreateDocumentFragment();
- xmlDocFragment.InnerXml =
- "<Fragment><SomeDate>Fragment Data</SomeDate></Fragment>";
-
- XmlElement rootNode = xmlDoc.DocumentElement;
- rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild);
-
- XmlNode node = xmlDoc.LastChild;
- 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
- public void MyNodeChangedEvent(object src,
- XmlNodeChangedEventArgs args) {
- MessageBox.Show("Node Changed Event Fired for node " + args.Node.Name);
- if (args.Node.Value != null) {
- MessageBox.Show(args.Node.Value);
- }
- }
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) {
- MessageBox.Show("Node Inserted event fired for node " + args.Node.Name);
- if (args.Node.Value != null) {
- MessageBox.Show(args.Node.Value);
- }
- }
Listing 9-14.The NodeRemoved event handler
- public void MyNodeRemovedEvent(object src,
- XmlNodeChangedEventArgs args) {
- MessageBox.Show("Node Removed event fired for node " + args.Node.Name);
- if (args.Node.Value != null) {
- MessageBox.Show(args.Node.Value);
- }
- }
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. |