Export (0) Print
Expand All

Event Handling in an XML Document Using the XmlNodeChangedEventArgs

The XmlNodeChangedEventArgs encapsulates the arguments passed to the event handlers registered on the XmlDocument object for handling events. The events and a description of when they are fired is given in the following table.

Event

Fired

NodeInserting

When a node belonging to the current document is about to be inserted into another node.

NodeInserted

When a node belonging to the current document has been inserted into another node.

NodeRemoving

When a node belonging to this document is about to be removed from the document.

NodeRemoved

When a node belonging to this document has been removed from its parent.

NodeChanging

When the value of a node is about to be changed.

NodeChanged

When the value of a node has been changed.

Note Note

If the XmlDataDocument memory usage is fully optimized to use DataSet storage, the XmlDataDocument might not raise any of the events listed above when changes are made to the underlying DataSet. If you need these events, you must traverse the whole XmlDocument once to make the memory usage non-fully optimized.

The following code example shows how to define an event handler and how to add the event handler to an event.

' Attach the event handler, NodeInsertedHandler, to the NodeInserted
' event.
Dim doc as XmlDocument = new XmlDocument()
Dim XmlNodeChgEHdlr as XmlNodeChangedEventHandler = new XmlNodeChangedEventHandler(addressof MyNodeChangedEvent) 
AddHandler doc.NodeInserted, XmlNodeChgEHdlr 

Dim n as XmlNode = doc.CreateElement( "element" )
Console.WriteLine( "Before Event Inserting" ) 

' This is the point where the new node is being inserted in the tree,
' and this is the point where the NodeInserted event is raised.
doc.AppendChild( n )
Console.WriteLine( "After Event Inserting" ) 

' Define the event handler that handles the NodeInserted event.
sub NodeInsertedHandler(src as Object, args as XmlNodeChangedEventArgs)
    Console.WriteLine("Node " + args.Node.Name + " inserted!!")
end sub

Some XML Document Object Model (DOM) operations are compound operations that can result in multiple events being fired. For example, AppendChild may also have to remove the node being appended from its previous parent. In this case, you see a NodeRemoved event fired first, followed by a NodeInserted event. Operations like setting InnerXml could result in multiple events.

The following code example shows the creation of the event handler and the handling of the NodeInserted event.

Imports System
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic

Public Class Sample

    Private Const filename As String = "books.xml"

    Shared Sub Main()
        Dim mySample As Sample = New Sample()
        mySample.Run(filename)
    End Sub

    Public Sub Run(ByVal args As String)
        ' Create and load the XML document.
        Console.WriteLine("Loading file 0 ...", args)
        Dim doc As XmlDocument = New XmlDocument()
        doc.Load(args)

        ' Create the event handlers.
        Dim XmlNodeChgEHdlr As XmlNodeChangedEventHandler = New XmlNodeChangedEventHandler(AddressOf MyNodeChangedEvent)
        Dim XmlNodeInsrtEHdlr As XmlNodeChangedEventHandler = New XmlNodeChangedEventHandler(AddressOf MyNodeInsertedEvent)
        AddHandler doc.NodeChanged, XmlNodeChgEHdlr
        AddHandler doc.NodeInserted, XmlNodeInsrtEHdlr

        ' Change the book price.
        doc.DocumentElement.LastChild.InnerText = "5.95"

        ' Add a new element.
        Dim newElem As XmlElement = doc.CreateElement("style")
        newElem.InnerText = "hardcover"
        doc.DocumentElement.AppendChild(newElem)

        Console.WriteLine(Chr(13) + Chr(10) + "Display the modified XML...")
        Console.WriteLine(doc.OuterXml)
    End Sub

    ' Handle the NodeChanged event.
    Public Sub MyNodeChangedEvent(ByVal src As Object, ByVal args As XmlNodeChangedEventArgs)
        Console.Write("Node Changed Event: <0> changed", args.Node.Name)
        If Not (args.Node.Value Is Nothing) Then
            Console.WriteLine(" with value  0", args.Node.Value)
        Else
            Console.WriteLine("")
        End If
    End Sub

    ' Handle the NodeInserted event.
    Public Sub MyNodeInsertedEvent(ByVal src As Object, ByVal args As XmlNodeChangedEventArgs)
        Console.Write("Node Inserted Event: <0> inserted", args.Node.Name)
        If Not (args.Node.Value Is Nothing) Then
            Console.WriteLine(" with value 0", args.Node.Value)
        Else
            Console.WriteLine("")
        End If
    End Sub

End Class        ' End class

For more information, see XmlNodeChangeEventArgs Members and XmlNodeChangedEventHandler Delegate.

Show:
© 2014 Microsoft