Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

XmlNodeChangedEventHandler Delegate

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

[Visual Basic]
<Serializable>
Public Delegate Sub XmlNodeChangedEventHandler( _
   ByVal sender As Object, _
   ByVal e As XmlNodeChangedEventArgs _
)
[C#]
[Serializable]
public delegate void XmlNodeChangedEventHandler(
   object sender,
   XmlNodeChangedEventArgs e
);
[C++]
[Serializable]
public __gc __delegate void XmlNodeChangedEventHandler(
   Object* sender,
   XmlNodeChangedEventArgs* e
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your event handler must have the same parameters as the XmlNodeChangedEventHandler delegate declaration.

sender
The source of the event.
e
An XmlNodeChangedEventArgs containing the event data.

Remarks

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.

Example

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

[Visual Basic] 
Imports System
Imports System.IO
Imports System.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(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.
  public sub MyNodeChangedEvent(src as Object, 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(src as Object, 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 

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample
{
  private const String filename = "book.xml";

  public static void Main()
  {
     Sample mySample = new Sample();
     mySample.Run(filename);
  }

  public void Run(String args)
  {
     
     // Create and load the XML document.
     Console.WriteLine ("Loading file {0} ...", args);
     XmlDocument doc = new XmlDocument();
     doc.Load (args);

     //Create the event handlers.
     doc.NodeChanged += new XmlNodeChangedEventHandler(this.MyNodeChangedEvent);
     doc.NodeInserted += new XmlNodeChangedEventHandler(this.MyNodeInsertedEvent);

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

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

     Console.WriteLine("\r\nDisplay the modified XML...");
     Console.WriteLine(doc.OuterXml);           

  }

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

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

} // End class 

[C++] 
#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>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Xml

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

System.Xml Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.