XmlDataDocument Events Working in VB.NET

In this article I will explain working with XmlDataDocument Events in ADO.NET.
  • 2608
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 Sub XmlNodeChangedEventHandler(ByVal senderAs Object,ByVal e As XmlNodeChangedEventArgs)

 
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 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 Sub XmlDocumentBtn_Click(ByVal sender As Object,ByVal e As System.EventArgs)
        Dim xmlDoc As New XmlDocument()
        xmlDoc.LoadXml("<Record> Some Value </Record>")

        ' Create the event handlers.
        AddHandler xmlDoc.NodeChanged,AddressOf Me.MyNodeChangedEvent
        AddHandler xmlDoc.NodeInserted,AddressOf Me.MyNodeInsertedEvent
        AddHandler xmlDoc.NodeRemoved,AddressOf Me.MyNodeRemovedEvent
        Dim root As XmlElement = xmlDoc.DocumentElement
        Dim str As String = root.ToString()
        Dim xmlDocFragmentAs XmlDocumentFragment = xmlDoc.CreateDocumentFragment()
        xmlDocFragment.InnerXml = "<Fragment><SomeDate>Fragment Data</SomeDate></Fragment>"

        ' Replace Node
        Dim rootNodeAs XmlElement = xmlDoc.DocumentElement
        rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild)

        'Remove Node
        Dim nodeAs XmlNode = xmlDoc.LastChild
        xmlDoc.RemoveChild(node)
   End Sub
 

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 Sub MyNodeChangedEvent(ByVal srcAs Object,ByVal args As XmlNodeChangedEventArgs)
        MessageBox.Show("Node Changed Event Fired for node " & args.Node.Name)
        If args.Node.ValueIsNot Nothing Then
            MessageBox.Show(args.Node.Value)
        End If
   End Sub
 
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 Sub MyNodeInsertedEvent(ByVal src As Object,ByVal args As XmlNodeChangedEventArgs)
        MessageBox.Show("Node Inserted event fired for node " & args.Node.Name)

        If args.Node.Value IsNot Nothing Then
            MessageBox.Show(args.Node.Value)
        End If
    End Sub


Listing 9-14.The NodeRemoved event handler


    Public Sub MyNodeRemovedEvent(ByVal src As Object,ByVal args As XmlNodeChangedEventArgs)
        MessageBox.Show("Node Removed event fired for node " & args.Node.Name)
        If args.Node.ValueIsNot Nothing Then
            MessageBox.Show(args.Node.Value)
        End If
   End Sub
 
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.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.