You must be wondering where XmlDataDocument comes from. Actually, this class works with Extensible Markup Language (XML) documents and is defined in the System.Xml namespace. So before testing this example, you must import the System.Xml namespace. (We discuss the XmlDataDocument object in Chapter 6.)
Note | You might want to skip this section if you're not familiar with the XmlDataDocument object. Chapter 6 covers XmlDataDocument. |
The XmlDataDocument events are useful when your application needs to be notified when changes are being made to an XmlDataDocument object. XmlDocument defines the XmlDataDocument events (see Table 5-6).
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 is inserting to another node |
NodeRemoved | Occurs when a node has been removed |
NodeRemoving | Occurs when a node is being removed |
The XmlNodeChangedEventHandler method handles these (NodeChanged, NodeChanging, NodeInserted, NodeInserting, NodeRemoved, and NodeRemoving) events. The XmlNodeChangedEventHandler is defined as follows:
Public Delegate Sub XmlNodeChangedEventHandler(_ ByVal sender As Object, _ ByVal e As XmlNodeChangedEventArgs _ )
In this example, sender is the source of the event and e is an XmlNodeChangedEventArgs containing the event data. The XmlNodeChangedEventArgs defines properties (see Table 5-7).
PROPERTY | DESCRIPTION |
---|---|
Action | Returns a value indicating the type of node-changed event |
NewParent | Returns the 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 operation started |
Listing 5-12 handles the XmlDataDocument events. The XmlDocumentBtn_Click method creates event handlers for NodeChanged, NodeInserted, and NodeRemoved events. MyNodeChangedEvent, MyNodeInsertedEvent, and MyNodeRemoved event handlers are executed when these events are fired. You use LoadXml to load an XML fragment and then use the ReplaceChild and RemoveChild methods to replace and remove document nodes.
Listing 5-12: The XmlDataDocument Event Handling Example
Private Sub XmlDataDocumentBtn_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles XmlDataDocumentBtn.Click Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.LoadXml("<Record> Some Value </Record>") ' Add event handlers AddHandler xmlDoc.NodeChanged, AddressOf NodeChangedEvent_Handler AddHandler xmlDoc.NodeInserted, AddressOf NodeInsertedEvent_Handler AddHandler xmlDoc.NodeRemoved, AddressOf NodeRemovedEvent_Handler Dim root As XmlElement = xmlDoc.DocumentElement Dim str As String = root.ToString() Dim xmlDocFragment As XmlDocumentFragment = xmlDoc.CreateDocumentFragment() xmlDocFragment.InnerXml = _ "<Fragment><SomeData>Fragment Data</SomeData></Fragment>" ' Replace Node Dim rootNode As XmlElement = xmlDoc.DocumentElement rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild) ' Remove Node Dim node As XmlNode = xmlDoc.LastChild xmlDoc.RemoveChild(node) ' Remove event handlers RemoveHandler xmlDoc.NodeChanged, AddressOf NodeChangedEvent_Handler RemoveHandler xmlDoc.NodeInserted, AddressOf NodeInsertedEvent_Handler RemoveHandler xmlDoc.NodeRemoved, AddressOf NodeRemovedEvent_Handler End Sub
Listing 5-13 shows the NodeChangedEvent handler. The Node property of XmlNodeChangedEventArgs returns an XmlNode object. Using the Node property you can get more information about a node such as its parent node, value, name, namespace, and so on. (See the "Using the XmlNode Class" section of Chapter 6 to learn more about XmlNode.)
Listing 5-13: The NodeChanged Event Handler
' NodeChanged event handler Public Sub NodeChangedEvent_Handler(ByVal src As Object, _ ByVal args As XmlNodeChangedEventArgs) MessageBox.Show("Node Changed Event Fired for node " + args.Node.Name) If args.Node.Value <> Nothing Then MessageBox.Show(args.Node.Value) End If End Sub
Similar to Listing 5-13, Listing 5-14 and Listing 5-15 show event handlers for the NodeInserted and NodeRemoved events.
Listing 5-14: The NodeInserted Event Handler
' NodeInserted event handler Public Sub NodeInsertedEvent_Handler(ByVal src As Object, _ ByVal args As XmlNodeChangedEventArgs) MessageBox.Show("Node Inserted Event Fired for node " + args.Node.Name) If args.Node.Value <> Nothing Then MessageBox.Show(args.Node.Value) End If End Sub
Listing 5-15: The NodeRemoved Event Handler
' Node Removed event handler Public Sub NodeRemovedEvent_Handler(ByVal src As Object, _ ByVal args As XmlNodeChangedEventArgs) MessageBox.Show("Node Removed Event Fired for node " + args.Node.Name) If args.Node.Value <> Nothing Then MessageBox.Show(args.Node.Value) End If End Sub