다음을 통해 공유


XmlSerializer.UnreferencedObject 이벤트

정의

SOAP로 인코딩된 XML 스트림의 deserialization을 수행하는 동안 XmlSerializer가 사용되지 않았거나 참조되지 않은 인식할 수 있는 형식을 발견하면 발생합니다.

public:
 event System::Xml::Serialization::UnreferencedObjectEventHandler ^ UnreferencedObject;
public event System.Xml.Serialization.UnreferencedObjectEventHandler UnreferencedObject;
member this.UnreferencedObject : System.Xml.Serialization.UnreferencedObjectEventHandler 
Public Custom Event UnreferencedObject As UnreferencedObjectEventHandler 

이벤트 유형

예제

다음 예제에서는 에 를 UnreferencedObjectEventHandler 추가합니다 XmlSerializer. 이벤트는 메서드에 Serializer``_``UnreferencedObject 의해 처리됩니다. 예제를 실행하려면 다음 XML을 잘라내어 "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.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;

[SoapInclude(Vehicle::typeid)]
public ref class Vehicle
{
public:
   String^ licenseNumber;
};


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

[SoapInclude(Vehicle::typeid)]
public ref class Group
{
public:
   String^ GroupName;
   Vehicle^ GroupVehicle;
};

public ref class Run
{
public:
   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 );
      reader->ReadStartElement();
      
      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( reader ));
      reader->ReadEndElement();
      reader->Close();
   }

private:
   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:
//  <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">
//<Vehicle id=S"id2" n1:type=S"Vehicle" 
//xmlns:n1=S"http://www.w3.org/2001/XMLSchema-instance">
//   <licenseNumber xmlns:q1=S"http://www.w3.org/2001/XMLSchema" 
//n1:type=S"q1:String*">ABCD</licenseNumber>
//<Vehicle id=S"id3" n1:type=S"Vehicle" 
//xmlns:n1=S"http://www.w3.org/2001/XMLSchema-instance">
//    <licenseNumber xmlns:q1=S"http://www.w3.org/2001/XMLSchema" 
//n1:type=S"q1:String*">1234</licenseNumber>
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:


//  <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>
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()
      test.DeserializeObject("UnrefObj.xml")
   End Sub
   
   Public Sub DeserializeObject(filename As String)
      ' Create an instance of the XmlSerializer class.
      Dim myMapping As XmlTypeMapping = _
      (new SoapReflectionImporter().ImportTypeMapping _
      (GetType(Group)))
      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)
      reader.ReadStartElement()

      ' Deserialize and cast the object.
      Dim myGroup As Group  
      myGroup = CType( mySerializer.Deserialize(reader), Group)
      reader.ReadEndElement()
      reader.Close()
   End Sub
   
   Private Sub Serializer_UnreferencedObject _
   (sender As object , e As UnreferencedObjectEventArgs)
      Console.WriteLine("UnreferencedObject:")
      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:'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>

설명

UnreferencedObject 이벤트는 이 World Wide Web 컨소시엄 문서 SOAP(Simple Object Access Protocol) 1.1의 섹션 5를 준수하는 SOAP 메시지를 포함하는 XML 문서를 역직렬화하는 데 사용되는 경우에만 발생 XmlSerializer 합니다.

섹션 5를 준수하는 문서는 특수한 형식입니다. 최소한 이러한 문서에는 SOAP 메시지의 기본 본문이 포함됩니다. 그러나 문서에서 인라인으로 정의된 모든 형식을 갖는 대신 일부 형식 정의를 문서의 최상위 요소에 대한 참조로 인코딩할 수 있습니다. 따라서 참조인 기본 본문에 있는 모든 요소에 대해 형식 정의를 포함하는 해당 요소가 있어야 합니다. 참조 요소와 형식 정의의 상관 관계를 지정하기 위해 형식 정의에는 id 고유한 문자열 ID로 설정된 특성이 있으며 참조 요소에는 href 동일한 ID를 참조하는 특성이 있습니다.

<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" n1:GroupName=".NET" GroupNumber="ZDI=" CreationDate="2002-05-02" xmlns:n1="http:'www.cpandl.com">
    <PosInt xsi:type="xsd:nonNegativeInteger">10000</PosInt>
    <GroupVehicle href="#id2" />
  </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">1234</licenseNumber>
  </Vehicle>

UnreferencedObject 이벤트는 문서에 형식 정의가 있지만 기본 본문에서 참조하는 매개 변수가 없는 경우에 발생합니다. 이벤트가 발생하면 클래스의 속성을 검사하여 참조되지 않은 개체의 UnreferencedObjectEventArgs XML 형식을 검색할 UnreferencedObject 수 있습니다. 개체의 XML ID는 속성에서 반환됩니다 UnreferencedId .

XML UnreferencedObject 노드 또는 요소 형식에 UnknownElement 해당하는 클래스 멤버가 없을 때 발생하는 및 이벤트와 UnknownNode 이벤트를 혼동해서는 안 됩니다.

적용 대상

추가 정보