Export (0) Print
Expand All
Expand Minimize

XmlNodeEventHandler Delegate

Represents the method that will handle the UnknownNode event of an XmlSerializer.

[Visual Basic]
<Serializable>
Public Delegate Sub XmlNodeEventHandler( _
   ByVal sender As Object, _
   ByVal e As XmlNodeEventArgs _
)
[C#]
[Serializable]
public delegate void XmlNodeEventHandler(
   object sender,
   XmlNodeEventArgs e
);
[C++]
[Serializable]
public __gc __delegate void XmlNodeEventHandler(
   Object* sender,
   XmlNodeEventArgs* 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 XmlNodeEventHandler delegate declaration.

sender
The source of the event.
e
An XmlNodeEventArgs that contains the event data.

Remarks

When you create an XmlNodeEventHandler delegate, you identify the method that will handle 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 UnknownNode event can occur only when you call the Deserialize method.

Example

[Visual Basic, C#, C++] The following example creates an XmlSerializer, adds an event handler for the UnknownNode event, and deserializes an object.

[Visual Basic] 
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 'DeserializeItem


Protected 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


[C#] 

    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);
    }
     
    protected 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);
    }


[C++] 

private:
    void DeserializeItem(String* filename) {
        XmlSerializer* mySerializer = 
            new XmlSerializer(__typeof(ObjectToDeserialize));
        // Add an instance of the delegate to the event.
        mySerializer->UnknownNode += new XmlNodeEventHandler(this, &Class1::Serializer_UnknownNode);
        // A FileStream is needed to read the file to deserialize.
        FileStream* fs = new FileStream(filename, FileMode::Open);
        ObjectToDeserialize* o = dynamic_cast<ObjectToDeserialize*>(mySerializer->Deserialize(fs));
    }
     
protected:
    void Serializer_UnknownNode(Object* sender, XmlNodeEventArgs* e) {
        
        Console::WriteLine(S"UnknownNode Name: {0}", e->Name);
        Console::WriteLine(S"UnknownNode LocalName: {0}", e->LocalName);
        Console::WriteLine(S"UnknownNode Namespace URI: {0}", e->NamespaceURI);
        Console::WriteLine(S"UnknownNode Text: {0}", e->Text);
     
        Object* o = e->ObjectBeingDeserialized;
        Console::WriteLine(S"Object being deserialized {0}", o);
           
        XmlNodeType myNodeType = e->NodeType;
        Console::WriteLine(myNodeType);
    }

[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.Serialization

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

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

See Also

System.Xml.Serialization Namespace | Deserialize | XmlSerializer

Show:
© 2015 Microsoft