Freigeben über


XmlNodeEventHandler Delegat

Definition

Stellt die Methode dar, die das UnknownNode-Ereignis einer XmlSerializer-Klasse behandelt.

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

Parameter

sender
Object

Die Quelle des Ereignisses.

e
XmlNodeEventArgs

Ein XmlNodeEventArgs, das die Ereignisdaten enthält.

Beispiele

Im folgenden Beispiel wird ein XmlSerializererstellt, ein -Ereignishandler für das UnknownNode -Ereignis hinzugefügt und ein -Objekt deserialisiert.

private:
   void DeserializeItem( String^ filename )
   {
      XmlSerializer^ mySerializer = gcnew XmlSerializer( ObjectToDeserialize::typeid );

      // Add an instance of the delegate to the event.
      mySerializer->UnknownNode += gcnew XmlNodeEventHandler( this, &Class1::Serializer_UnknownNode );

      // A FileStream is needed to read the file to deserialize.
      FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
      ObjectToDeserialize^ o = dynamic_cast<ObjectToDeserialize^>(mySerializer->Deserialize( fs ));
   }

   void Serializer_UnknownNode( Object^ sender, XmlNodeEventArgs^ e )
   {
      Console::WriteLine( "UnknownNode Name: {0}", e->Name );
      Console::WriteLine( "UnknownNode LocalName: {0}", e->LocalName );
      Console::WriteLine( "UnknownNode Namespace URI: {0}", e->NamespaceURI );
      Console::WriteLine( "UnknownNode Text: {0}", e->Text );
      Object^ o = e->ObjectBeingDeserialized;
      Console::WriteLine( "Object being deserialized {0}", o );
      XmlNodeType myNodeType = e->NodeType;
      Console::WriteLine( myNodeType );
   }

private void DeserializeItem(string filename) {
    XmlSerializer mySerializer =
        new XmlSerializer(typeof(ObjectToDeserialize));
    // Add an instance of the delegate to the event.
    mySerializer.UnknownNode += new XmlNodeEventHandler
        (Serializer_UnknownNode);
    // A FileStream is needed to read the file to deserialize.
    FileStream fs = new FileStream(filename, FileMode.Open);
    ObjectToDeserialize o = (ObjectToDeserialize)mySerializer.Deserialize(fs);
}

private void Serializer_UnknownNode
    (object sender, XmlNodeEventArgs e) {

    Console.WriteLine("UnknownNode Name: "
                      + e.Name);
    Console.WriteLine("UnknownNode LocalName: "
                      + e.LocalName);
    Console.WriteLine("UnknownNode Namespace URI: "
                      + e.NamespaceURI);
    Console.WriteLine("UnknownNode Text: "
                      + e.Text);

    object o = e.ObjectBeingDeserialized;
    Console.WriteLine("Object being deserialized "
                      + o.ToString());

    XmlNodeType myNodeType = e.NodeType;
    Console.WriteLine(myNodeType);
}
Private Sub DeserializeItem(ByVal filename As String)
    Dim mySerializer As New XmlSerializer(GetType(ObjectToDeserialize))
    ' Add an instance of the delegate to the event.
    AddHandler mySerializer.UnknownNode, AddressOf Serializer_UnknownNode
    ' A FileStream is needed to read the file to deserialize.
    Dim fs As New FileStream(filename, FileMode.Open)
    Dim o As ObjectToDeserialize = _
        CType(mySerializer.Deserialize(fs), ObjectToDeserialize)
End Sub


Private Sub Serializer_UnknownNode _
                (ByVal sender As Object, _
                 ByVal e As XmlNodeEventArgs)
    
    Console.WriteLine("UnknownNode Name: " & e.Name)
    Console.WriteLine("UnknownNode LocalName: " & e.LocalName)
    Console.WriteLine("UnknownNode Namespace URI: " & e.NamespaceURI)
    Console.WriteLine("UnknownNode Text: " & e.Text)
    
    Dim o As Object = e.ObjectBeingDeserialized
    Console.WriteLine("Object being deserialized " & o.ToString())
    
    Dim myNodeType As XmlNodeType = e.NodeType
    Console.WriteLine(myNodeType)
End Sub

Hinweise

Wenn Sie einen XmlNodeEventHandler Delegaten erstellen, identifizieren Sie die Methode, die das Ereignis behandelt. Um dem Ereignishandler das Ereignis zuzuordnen, fügen Sie dem Ereignis eine Instanz des Delegaten hinzu. Der Ereignishandler wird bei jedem Eintreten des Ereignisses aufgerufen, sofern der Delegat nicht entfernt wird. Weitere Informationen zu Ereignishandlerdelegaten finden Sie unter Behandeln und Auslösen von Ereignissen.

Das UnknownNode Ereignis tritt nur auf, wenn Sie die Deserialize -Methode aufrufen.

Erweiterungsmethoden

GetMethodInfo(Delegate)

Ruft ein Objekt ab, das die Methode darstellt, die vom angegebenen Delegaten dargestellt wird.

Gilt für:

Weitere Informationen