XmlNodeEventHandler Delegar

Definição

Representa o método que manipula o evento UnknownNode de um XmlSerializer.Represents the method that handles the UnknownNode event of an XmlSerializer.

public delegate void XmlNodeEventHandler(System::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)

Parâmetros

sender
Object

A fonte do evento.The source of the event.

e
XmlNodeEventArgs

Um XmlNodeEventArgs que contém os dados do evento.An XmlNodeEventArgs that contains the event data.

Herança
XmlNodeEventHandler

Exemplos

O exemplo a seguir cria XmlSerializerum, adiciona um manipulador de eventos UnknownNode para o evento e desserializa um objeto.The following example creates an XmlSerializer, adds an event handler for the UnknownNode event, and deserializes an object.

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

Comentários

Ao criar um XmlNodeEventHandler delegado, você identifica o método que manipula o evento.When you create an XmlNodeEventHandler delegate, you identify the method that handles the event. Para associar o evento ao manipulador de eventos, adicione uma instância do delegado ao evento.To associate the event with your event handler, add an instance of the delegate to the event. O manipulador de eventos é chamado sempre que o evento ocorre, a menos que você remova o representante.The event handler is called whenever the event occurs, unless you remove the delegate. Para obter mais informações sobre delegados de manipulador de eventos, consulte manipulando e gerando eventos.For more information about event handler delegates, see Handling and Raising Events.

O UnknownNode evento ocorre somente quando você chama o Deserialize método.The UnknownNode event occurs only when you call the Deserialize method.

Métodos de Extensão

GetMethodInfo(Delegate)

Obtém um objeto que representa o método representado pelo delegado especificado.Gets an object that represents the method represented by the specified delegate.

Aplica-se a

Veja também