Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

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
)

Parameters

sender
Type: System.Object

The source of the event.

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

 <wrapper>
   <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">
    </Group>
 <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>
 <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>
   </Vehicle>
 </wrapper>
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.
[SoapInclude(typeof(Vehicle))]
public class Group
{
   public string GroupName;
   public Vehicle GroupVehicle;
}
 [SoapInclude(typeof(Vehicle))]
public class Vehicle
{
   public string licenseNumber;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.DeserializeObject("UnrefObj.xml");
   }

   public 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
      (Serializer_UnreferencedObject);

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

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

   private void Serializer_UnreferencedObject
   (object sender, UnreferencedObjectEventArgs e){
      Console.WriteLine("UnreferencedObject:");
      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="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">
//   </Group> 

//<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> 

//<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>
//  </Vehicle> 

//</wrapper>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.