次の方法で共有


UnreferencedObjectEventArgs クラス

エンコード済みの SOAP XML ストリームで逆シリアル化時に見つかった、既知ではあっても未参照のオブジェクトに対してデータを提供します。

この型のすべてのメンバの一覧については、UnreferencedObjectEventArgs メンバ を参照してください。

System.Object
   System.EventArgs
      System.Xml.Serialization.UnreferencedObjectEventArgs

Public Class UnreferencedObjectEventArgs
   Inherits EventArgs
[C#]
public class UnreferencedObjectEventArgs : EventArgs
[C++]
public __gc class UnreferencedObjectEventArgs : public EventArgs
[JScript]
public class UnreferencedObjectEventArgs extends EventArgs

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

UnreferencedObjectEventArgs クラスの詳細については、 XmlSerializerUnreferencedObject イベントのトピックを参照してください。

UnreferencedObject イベントは、 Deserialize メソッドが呼び出されたときにだけ発生します。

イベント処理の詳細については、「 イベントの利用 」を参照してください。

使用例

[Visual Basic, C#, C++] UnreferencedObjectEventHandlerXmlSerializer に追加する例を次に示します。イベントは、 Serializer_UnreferencedObject メソッドによって処理されます。このサンプル コードを実行するには、UnrefObj.xml という名前のファイルに、次の XML を切り取って貼り付けます。

 
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()
      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:

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


[C#] 
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>


[C++] 
#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;
[SoapInclude(__typeof(Vehicle))]
__gc public class Vehicle
{
public:
   String* licenseNumber;
};

// You must use the SoapIncludeAttribute to inform the XmlSerializer
// that the Vehicle type should be recognized when deserializing.
[SoapInclude(__typeof(Vehicle))]
__gc public class Group
{
public:
   String* GroupName;
   Vehicle * GroupVehicle;
};

__gc public class Run 
{
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
         (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();
   }

private:
   void Serializer_UnreferencedObject(Object* /*sender*/, UnreferencedObjectEventArgs * e) 
   {
      Console::WriteLine(S"UnreferencedObject:");
      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"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" 
//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>

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

//</wrapper>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Xml.Serialization

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Xml (System.Xml.dll 内)

参照

UnreferencedObjectEventArgs メンバ | System.Xml.Serialization 名前空間