次の方法で共有


XmlNodeChangedEventHandler 代理人

定義

NodeChangedNodeChangingNodeInsertedNodeInsertingNodeRemovedNodeRemoving の各イベントを処理するメソッドを表します。

public delegate void XmlNodeChangedEventHandler(System::Object ^ sender, XmlNodeChangedEventArgs ^ e);
public delegate void XmlNodeChangedEventHandler(object sender, XmlNodeChangedEventArgs e);
type XmlNodeChangedEventHandler = delegate of obj * XmlNodeChangedEventArgs -> unit
Public Delegate Sub XmlNodeChangedEventHandler(sender As Object, e As XmlNodeChangedEventArgs)

パラメーター

sender
Object

イベントのソース。

e
XmlNodeChangedEventArgs

イベント データを格納している XmlNodeChangedEventArgs

次の例は、 イベントと NodeInserted イベントを処理する方法をNodeChanged示しています。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
public ref class Sample
{
public:
   void Run( String^ args )
   {
      // Create and load the XML document.
      Console::WriteLine( "Loading file {0} ...", args );
      XmlDocument^ doc = gcnew XmlDocument;
      doc->Load( args );
      
      //Create the event handlers.
      doc->NodeChanged += gcnew XmlNodeChangedEventHandler( this, &Sample::MyNodeChangedEvent );
      doc->NodeInserted += gcnew XmlNodeChangedEventHandler( this, &Sample::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^ /*src*/, XmlNodeChangedEventArgs^ args )
   {
      Console::Write( "Node Changed Event: <{0}> changed", args->Node->Name );
      if ( args->Node->Value != nullptr )
      {
         Console::WriteLine( " with value  {0}", args->Node->Value );
      }
      else
            Console::WriteLine( "" );
   }

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

int main()
{
   Sample^ mySample = gcnew Sample;
   mySample->Run( "book.xml" );
}
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
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

この例では、 ファイル book.xml を入力として使用します。

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

注釈

デリゲートを XmlNodeChangedEventHandler 作成するときは、イベントを処理するメソッドを識別します。 イベント ハンドラーにイベントを関連付けるには、イベントにデリゲートのインスタンスを追加します。 イベント ハンドラーは、デリゲートを削除しない限り、イベントが発生するたびに呼び出されます。 イベント ハンドラー デリゲートの詳細については、「イベントの 処理と発生」を参照してください。

拡張メソッド

GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象