UnreferencedObjectEventHandler Delegate


Represents the method that handles the UnreferencedObject event of an XmlSerializer.

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

public delegate void UnreferencedObjectEventHandler(
	Object^ sender,
	UnreferencedObjectEventArgs^ e


Type: System::Object^

The source of the event.

Type: System.Xml.Serialization::UnreferencedObjectEventArgs^

An UnreferencedObjectEventArgs that contains the event data.

When you create an UnreferencedObjectEventHandler delegate, you identify the method that handles 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 NIB: Events and Delegates

The UnreferencedObject event occurs only when you call the Deserialize method.

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="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" n1:GroupName=".NET" xmlns:n1="http://www.cpandl.com">
<Vehicle id="id2" n1:type="Vehicle" xmlns:n1="http://www.w3.org/2001/XMLSchema-instance">
    <licenseNumber xmlns:q1="http://www.w3.org/2001/XMLSchema" n1:type="q1:string">ABCD</licenseNumber>
<Vehicle id="id3" n1:type="Vehicle" xmlns:n1="http://www.w3.org/2001/XMLSchema-instance">
    <licenseNumber xmlns:q1="http://www.w3.org/2001/XMLSchema" n1:type="q1:string">1234</licenseNumber>
#using <System.Xml.dll>
#using <System.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;

ref class Vehicle;

public ref class Vehicle
   String^ licenseNumber;

// You must use the SoapIncludeAttribute to inform the XmlSerializer
// that the Vehicle type should be recognized when deserializing.

public ref class Group
   String^ GroupName;
   Vehicle^ GroupVehicle;

public ref class Run
   void DeserializeObject( String^ filename )
      // Create an instance of the XmlSerializer class.
      XmlTypeMapping^ myMapping = ((gcnew SoapReflectionImporter)->ImportTypeMapping( Group::typeid ));
      XmlSerializer^ mySerializer = gcnew XmlSerializer( myMapping );
      mySerializer->UnreferencedObject += gcnew UnreferencedObjectEventHandler( this, &Run::Serializer_UnreferencedObject );

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

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

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

int main()
   Run^ test = gcnew Run;
   test->DeserializeObject( "UnrefObj.xml" );

// The file named S"UnrefObj.xml" should contain this XML:
// <wrapper>
//  <Group xmlns:xsi=S"http://www.w3.org/2001/XMLSchema-instance" 
//xmlns:xsd=S"http://www.w3.org/2001/XMLSchema" id=S"id1" 
//n1:GroupName=S".NET" xmlns:n1=S"http://www.cpandl.com">
//   </Group>
//<Vehicle id=S"id2" n1:type=S"Vehicle" 
//   <licenseNumber xmlns:q1=S"http://www.w3.org/2001/XMLSchema" 
//   </Vehicle>
//<Vehicle id=S"id3" n1:type=S"Vehicle" 
//    <licenseNumber xmlns:q1=S"http://www.w3.org/2001/XMLSchema" 
//  </Vehicle>

.NET Framework
Available since 1.1
Return to top