Export (0) Print
Expand All
Expand Minimize
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.

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

public delegate void XmlNodeChangedEventHandler (
	Object sender,
	XmlNodeChangedEventArgs e
)
/** @delegate */
public delegate void XmlNodeChangedEventHandler (
	Object sender, 
	XmlNodeChangedEventArgs e
)
Not applicable.

Parameters

sender

The source of the event.

e

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.

using System;
using System.IO;
using System.Xml;

namespace Microsoft.Samples.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.
        private void MyNodeChangedEvent(Object source, 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.
        private void MyNodeInsertedEvent(Object source, 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 

import System.*;
import System.IO.*;
import System.Xml.*;

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

	public static void main(String[] args)
	{
		Sample mySample = new Sample();
		mySample.Run(filename);
	} //main

	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.add_NodeChanged(new XmlNodeChangedEventHandler(this.
			MyNodeChangedEvent));
		doc.add_NodeInserted(new XmlNodeChangedEventHandler(this.
			MyNodeInsertedEvent));
		// Change the book price.
		doc.get_DocumentElement().get_LastChild().set_InnerText("5.95");
		// Add a new element.
		XmlElement newElem = doc.CreateElement("style");
		newElem.set_InnerText("hardcover");
		doc.get_DocumentElement().AppendChild(newElem);

		Console.WriteLine("\r\nDisplay the modified XML...");
		Console.WriteLine(doc.get_OuterXml());
	} //Run

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

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

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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.