This documentation is archived and is not being maintained.

UnreferencedObjectEventHandler Delegate

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

[Visual Basic]
Public Delegate Sub UnreferencedObjectEventHandler( _
   ByVal sender As Object, _
   ByVal e As UnreferencedObjectEventArgs _
public delegate void UnreferencedObjectEventHandler(
   object sender,
   UnreferencedObjectEventArgs e
public __gc __delegate void UnreferencedObjectEventHandler(
   Object* sender,
   UnreferencedObjectEventArgs* 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 UnreferencedObjectEventHandler delegate declaration.

The source of the event.
An UnreferencedObjectEventArgs that contains the event data.


When you create an UnreferencedObjectEventHandler 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 UnreferencedObject event can occur only when you call the Deserialize method.


[Visual Basic, C#, C++] The following example adds an UnreferencedObjectEventHandler to an XmlSerializer. The event is handled by the Serializer_UnreferencedObject method. To run the example, cut and paste the following XML into a file named UnrefObj.xml.

  <Group xmlns:xsi="" xmlns:xsd="" id="id1" n1:GroupName=".NET" xmlns:n1="">
<Vehicle id="id2" n1:type="Vehicle" xmlns:n1="">
    <licenseNumber xmlns:q1="" n1:type="q1:string">ABCD</licenseNumber>
<Vehicle id="id3" n1:type="Vehicle" xmlns:n1="">
    <licenseNumber xmlns:q1="" n1:type="q1:string">1234</licenseNumber>
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Schema

' You must use the SoapIncludeAttribute to inform the XmlSerializer
' that the Vehicle type should be recognized when deserializing.
<SoapInclude(GetType(Vehicle))> _
Public Class Group
    Public GroupName As String 
   public GroupVehicle As Vehicle 
End Class

Public Class Vehicle
   Public licenseNumber As String 
End Class

Public Class Run
   Shared Sub Main()
      Dim test As Run = new Run()
   End Sub
   Public Sub DeserializeObject(filename As String)
      ' Create an instance of the XmlSerializer class.
      Dim myMapping As XmlTypeMapping = _
      (new SoapReflectionImporter().ImportTypeMapping _
      Dim mySerializer As XmlSerializer =  _
      new XmlSerializer(myMapping)

      AddHandler mySerializer.UnreferencedObject, _
      AddressOf Serializer_UnreferencedObject

      ' Reading the file requires an  XmlTextReader.
      Dim reader As XmlTextReader = _
      new XmlTextReader(filename)

      ' Deserialize and cast the object.
      Dim myGroup As Group  
      myGroup = CType( mySerializer.Deserialize(reader), Group)
   End Sub
   Private Sub Serializer_UnreferencedObject _
   (sender As object , e As UnreferencedObjectEventArgs)
      Console.WriteLine("ID: " + e.UnreferencedId)
      Console.WriteLine("UnreferencedObject: " + e.UnreferencedObject)
      Dim myVehicle As Vehicle = CType(e.UnreferencedObject, Vehicle)
      Console.WriteLine("License: " + myVehicle.licenseNumber)
       End Sub
 End Class
' The XML document should contain this information:


'  <Group xmlns:xsi="http:'" 
'xmlns:xsd="http:'" id="id1" 
'n1:GroupName=".NET" xmlns:n1="http:'">
'   </Group>

'<Vehicle id="id2" n1:type="Vehicle" 
'    <licenseNumber xmlns:q1="http:'" 
'  </Vehicle>

'<Vehicle id="id3" n1:type="Vehicle" 
'    <licenseNumber xmlns:q1="http:'" 
'  </Vehicle>


using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

// You must use the SoapIncludeAttribute to inform the XmlSerializer
// that the Vehicle type should be recognized when deserializing.
public class Group
   public string GroupName;
   public Vehicle GroupVehicle;
public class Vehicle
   public string licenseNumber;

public class Run
   public static void Main()
      Run test = new Run();
   public void DeserializeObject(string filename)
      // Create an instance of the XmlSerializer class.
      XmlTypeMapping myMapping = 
      (new SoapReflectionImporter().ImportTypeMapping(
      XmlSerializer mySerializer =  
      new XmlSerializer(myMapping);
      mySerializer.UnreferencedObject += 
      new UnreferencedObjectEventHandler

      // Reading the file requires an  XmlTextReader.
      XmlTextReader reader= 
      new XmlTextReader(filename);

      // Deserialize and cast the object.
      Group myGroup; 
      myGroup = (Group) mySerializer.Deserialize(reader);

   private void Serializer_UnreferencedObject
   (object sender, UnreferencedObjectEventArgs e){
      Console.WriteLine("ID: " + e.UnreferencedId);
      Console.WriteLine("UnreferencedObject: " + e.UnreferencedObject);
      Vehicle myVehicle = (Vehicle) e.UnreferencedObject;
      Console.WriteLine("License: " + myVehicle.licenseNumber);

// The file named "UnrefObj.xml" should contain this XML:

// <wrapper>

//  <Group xmlns:xsi="" 
//xmlns:xsd="" id="id1" 
//n1:GroupName=".NET" xmlns:n1="">
//   </Group>

//<Vehicle id="id2" n1:type="Vehicle" 
//   <licenseNumber xmlns:q1="" 
//   </Vehicle>

//<Vehicle id="id3" n1:type="Vehicle" 
//    <licenseNumber xmlns:q1="" 
//  </Vehicle>


#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::Xml::Schema;

__gc public class Vehicle;
__gc public class Vehicle
   String* licenseNumber;

// You must use the SoapIncludeAttribute to inform the XmlSerializer
// that the Vehicle type should be recognized when deserializing.
__gc public class Group
   String* GroupName;
   Vehicle * GroupVehicle;

__gc public class Run 
   void DeserializeObject(String* filename) 
      // Create an instance of the XmlSerializer class.
      XmlTypeMapping * myMapping = 
         ((new SoapReflectionImporter()) -> ImportTypeMapping(__typeof(Group)));
      XmlSerializer* mySerializer = new XmlSerializer(myMapping);

      mySerializer -> UnreferencedObject += new UnreferencedObjectEventHandler
         (this, &Run::Serializer_UnreferencedObject);

      // Reading the file requires an  XmlTextReader.
      XmlTextReader* reader = new XmlTextReader(filename);
      reader -> ReadStartElement();

      // Deserialize and cast the object.
      Group * myGroup; 
      myGroup = dynamic_cast<Group*>(mySerializer -> Deserialize(reader));
      reader -> ReadEndElement();
      reader -> Close();

   void Serializer_UnreferencedObject(Object* /*sender*/, UnreferencedObjectEventArgs * e) 
      Console::WriteLine(S"ID: {0}", e -> UnreferencedId);
      Console::WriteLine(S"UnreferencedObject: {0}", e -> UnreferencedObject);
      Vehicle * myVehicle = dynamic_cast<Vehicle*>(e -> UnreferencedObject);
      Console::WriteLine(S"License: {0}", myVehicle -> licenseNumber);

int main() 
   Run* test = new Run();
   test -> DeserializeObject(S"UnrefObj.xml");

// The file named S"UnrefObj.xml" should contain this XML:

// <wrapper>

//  <Group xmlns:xsi=S"" 
//xmlns:xsd=S"" id=S"id1" 
//n1:GroupName=S".NET" xmlns:n1=S"">
//   </Group>

//<Vehicle id=S"id2" n1:type=S"Vehicle" 
//   <licenseNumber xmlns:q1=S"" 
//   </Vehicle>

//<Vehicle id=S"id3" n1:type=S"Vehicle" 
//    <licenseNumber xmlns:q1=S"" 
//  </Vehicle>


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


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