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

UnreferencedObjectEventArgs Class

Provides data for the known, but unreferenced, object found in an encoded SOAP XML stream during deserialization.

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

public class UnreferencedObjectEventArgs : EventArgs
public class UnreferencedObjectEventArgs extends EventArgs
public class UnreferencedObjectEventArgs extends EventArgs
Not applicable.

For more information about the UnreferencedObjectEventArgs class, see the UnreferencedObject event of the XmlSerializer.

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

For more information about handling events, see Events Overview (Windows Forms)

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>


import System.*;
import System.IO.*;
import System.Text.*;
import System.Xml.*;
import System.Xml.Serialization.*;
import System.Xml.Schema.*;
// You must use the SoapIncludeAttribute to inform the XmlSerializer
// that the Vehicle type should be recognized when deserializing.
/** @attribute SoapInclude(Vehicle.class)
 */
public class Group
{
    public String groupName;
    public Vehicle groupVehicle;
} //Group

/** @attribute SoapInclude(Vehicle.class)
 */
public class Vehicle
{
    public String licenseNumber;
} //Vehicle

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.DeserializeObject("UnrefObj.xml");
    } //main

    public void DeserializeObject(String fileName)
    {
        // Create an instance of the XmlSerializer class.
        XmlTypeMapping myMapping = (new SoapReflectionImporter()).
            ImportTypeMapping(Group.class.ToType());

        XmlSerializer mySerializer = new XmlSerializer(myMapping);

        mySerializer.add_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();
    } //DeserializeObject

    private void Serializer_UnreferencedObject(Object sender,
        UnreferencedObjectEventArgs e)
    {
        Console.WriteLine("UnreferencedObject:");
        Console.WriteLine("ID: " + e.get_UnreferencedId());
        Console.WriteLine("UnreferencedObject: " + e.get_UnreferencedObject());
        Vehicle myVehicle = (Vehicle)e.get_UnreferencedObject();
        Console.WriteLine("License: " + myVehicle.licenseNumber);
    } //Serializer_UnreferencedObject
} //Run

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

System.Object
   System.EventArgs
    System.Xml.Serialization.UnreferencedObjectEventArgs
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.