XmlNodeChangedEventHandler Delegate
TOC
Collapse the table of content
Expand the table of content

XmlNodeChangedEventHandler Delegate

Represents the method that handles NodeChanged, NodeChanging, NodeInserted, NodeInserting, NodeRemoved and NodeRemoving events.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

'Declaration
Public Delegate Sub XmlNodeChangedEventHandler ( _
	sender As Object, _
	e As XmlNodeChangedEventArgs _
)
'Usage
Dim instance As New XmlNodeChangedEventHandler(AddressOf HandlerMethod)

Parameters

sender
Type: System.Object

The source of the event.

e
Type: System.Xml.XmlNodeChangedEventArgs

An XmlNodeChangedEventArgs containing the event data.

When you create an XmlNodeChangedEventHandler delegate, you identify the method that handles the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.

The following example shows how to handle the NodeChanged and NodeInserted events.

Imports System
Imports System.IO
Imports System.Xml

Namespace Microsoft.Samples.Xml

    Public Class Sample

        Private Const filename As String = "book.xml" 

        Public 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. 
            AddHandler doc.NodeChanged, AddressOf MyNodeChangedEvent
            AddHandler doc.NodeInserted, AddressOf MyNodeInsertedEvent

            ' 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()
            Console.WriteLine("Display the modified XML...")
            Console.WriteLine(doc.OuterXml)

        End Sub 

        ' Handle the NodeChanged event. 
        Private Sub MyNodeChangedEvent(ByVal source 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. 
        Private Sub MyNodeInsertedEvent(ByVal source 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 Namespace
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

public __gc class Sample
{
public:
  void Run(String* args)
  {

     // Create and load the XML document.
     Console::WriteLine (S"Loading file {0} ...", args);
     XmlDocument* doc = new XmlDocument();
     doc->Load (args);

     //Create the event handlers.
     doc->NodeChanged += new XmlNodeChangedEventHandler(this, &Sample::MyNodeChangedEvent);
     doc->NodeInserted += new XmlNodeChangedEventHandler(this, &Sample::MyNodeInsertedEvent);

     // Change the book price.
     doc->DocumentElement->LastChild->InnerText = S"5.95";

     // Add a new element.
     XmlElement* newElem = doc->CreateElement(S"style");
     newElem->InnerText = S"hardcover";
     doc->DocumentElement->AppendChild(newElem);

     Console::WriteLine(S"\r\nDisplay the modified XML...");
     Console::WriteLine(doc->OuterXml);           

  }

  // Handle the NodeChanged event.
  void MyNodeChangedEvent(Object* /*src*/, XmlNodeChangedEventArgs* args)
  {
     Console::Write(S"Node Changed Event: <{0}> changed", args->Node->Name);
     if (args->Node->Value != 0)
     {
        Console::WriteLine(S" with value  {0}", args->Node->Value);
     }
     else
       Console::WriteLine(S"");
  }

  // Handle the NodeInserted event.
  void MyNodeInsertedEvent(Object* /*src*/, XmlNodeChangedEventArgs* args)
  {
     Console::Write(S"Node Inserted Event: <{0}> inserted", args->Node->Name);
     if (args->Node->Value != 0)
     {
        Console::WriteLine(S" with value {0}", args->Node->Value);
     }
     else
        Console::WriteLine(S"");
  }

}; // End class 

int main()
{
   Sample* mySample = new Sample();
   mySample->Run(S"book.xml");
}

The example uses the file book.xml as input.

<!--sample XML fragment-->
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
  <title>The Handmaid's Tale</title>
  <price>14.95</price>
</book>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft