次の方法で共有


SoapTypeAttribute クラス

SOAP エンコード済みの XML としてクラス インスタンスがシリアル化されるときに、 XmlSerializer によって生成されるスキーマを制御します。

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

System.Object
   System.Attribute
      System.Xml.Serialization.SoapTypeAttribute

<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Struct _
   Or AttributeTargets.Enum Or AttributeTargets.Interface)>
Public Class SoapTypeAttribute   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct |
   AttributeTargets.Enum | AttributeTargets.Interface)]
public class SoapTypeAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Class | AttributeTargets::Struct
   | AttributeTargets::Enum | AttributeTargets::Interface)]
public __gc class SoapTypeAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct |
   AttributeTargets.Enum | AttributeTargets.Interface)
class SoapTypeAttribute extends Attribute

スレッドセーフ

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

解説

SoapIgnoreAttribute クラスは、 XmlSerializer がオブジェクトをエンコード済み SOAP XML としてシリアル化または逆シリアル化する方法を制御する一連の属性のうちの 1 つです。結果として生成される XML は、W3C (World Wide Web Consortium) (www.w3.org) のドキュメント『Simple Object Access Protocol (SOAP) 1.1』のセクション 5 に準拠します。類似する属性の完全な一覧については、「 エンコード済み SOAP シリアル化を制御する属性 」を参照してください。

オブジェクトをエンコード済み SOAP メッセージとしてシリアル化するには、 SoapReflectionImporter クラスの ImportTypeMapping メソッドで作成された XmlTypeMapping を使用して、 XmlSerializer を構築する必要があります。

SoapTypeAttribute は、クラス宣言にだけ適用できます。

IncludeInSchema プロパティは、結果として生成される XML 要素型が、生成される XML ストリームの XML スキーマ ドキュメント (.xsd) に含まれるかどうかを指定します。スキーマを参照するには、このクラスをコンパイルして DLL ファイルを生成します。作成されるファイルを引数として XML スキーマ定義ツール (Xsd.exe) に渡します。このツールは、 XmlSerializer クラスのインスタンスによってクラスがシリアル化されるときに生成される XML ストリームの XML スキーマを作成します。

別の名前空間を設定すると、Xsd.exe は、クラスがシリアル化されるときに生成される XML について、別のスキーマ (.xsd) ファイルを作成します。

使用例

[Visual Basic, C#, C++] Group という名前のクラスをシリアル化する例を次に示します。 TypeName を "SoapGroupType" に設定した SoapTypeAttribute がクラスに適用されています。この SoapTypeAttribute はオーバーライドされ、 TypeName が "Team" に変更されます。両方のバージョンがシリアル化され、結果として SoapType.xml と SoapType2.xml の 2 つのファイルが作成されます。

 
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

' The SoapType is overridden when the
' SerializeOverride  method is called.
<SoapType("SoapGroupType", "http://www.cohowinery.com")> _
Public class Group
   Public GroupName As String
   Public Employees() As Employee
End Class

<SoapType("EmployeeType")> _
Public Class Employee
   Public Name As String
End Class
   
Public class Run
   Public Shared Sub Main()
      Dim test As Run = New Run()
      test.SerializeOriginal("SoapType.xml")
      test.SerializeOverride("SoapType2.xml")
      test.DeserializeObject("SoapType2.xml")
   End Sub

   Public Sub SerializeOriginal(filename As String )
      ' Create an instance of the XmlSerializer class that
      ' can be used for serializing as a SOAP message.
     Dim mapp  As XmlTypeMapping = _
      (New SoapReflectionImporter()).ImportTypeMapping(GetType(Group))
      Dim mySerializer As XmlSerializer =  _
      New XmlSerializer(mapp)
      
      ' Writing the file requires a TextWriter.
      Dim writer As TextWriter = New StreamWriter(filename)

      ' Create an instance of the class that will be serialized.
      Dim myGroup As Group = New Group()

      ' Set the object properties.
      myGroup.GroupName = ".NET"
      Dim e1 As Employee = New Employee()
      e1.Name = "Pat"
      myGroup.Employees=New Employee(){e1}

      ' Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup)
      writer.Close()
   End Sub

   Public Sub SerializeOverride(filename As string )
   
      ' Create an instance of the XmlSerializer class that
      ' uses a SoapAttributeOverrides object.
      Dim mySerializer As XmlSerializer =  CreateOverrideSerializer()

      ' Writing the file requires a TextWriter.
      Dim writer As TextWriter = New StreamWriter(filename)

      ' Create an instance of the class that will be serialized.
      Dim myGroup As Group = New Group()

      ' Set the object properties.
      myGroup.GroupName = ".NET"
      Dim e1 As Employee = New Employee()
      e1.Name = "Pat"
      myGroup.Employees=New Employee(){e1}
       ' Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup)
       writer.Close()
   End Sub

   Private Function CreateOverrideSerializer() As XmlSerializer 
      ' Create and return an XmlSerializer instance used to
      ' override and create SOAP messages.
      Dim mySoapAttributeOverrides As SoapAttributeOverrides = _
          New SoapAttributeOverrides()
      Dim soapAtts As SoapAttributes = New SoapAttributes()

      ' Override the SoapTypeAttribute.
      Dim soapType As SoapTypeAttribute = New SoapTypeAttribute()
      soapType.TypeName = "Team"
      soapType.IncludeInSchema = false
      soapType.Namespace = "https://www.microsoft.com"
      soapAtts.SoapType = soapType
      
      mySoapAttributeOverrides.Add(GetType(Group),soapAtts)

      ' Create an XmlTypeMapping that is used to create an instance 
      ' of the XmlSerializer. Then return the XmlSerializer object.
      Dim myMapping As XmlTypeMapping = (New SoapReflectionImporter( _
      mySoapAttributeOverrides)).ImportTypeMapping(GetType(Group))
    
      Dim  ser As XmlSerializer = New XmlSerializer(myMapping)
      
      return ser
   End Function

   Public Sub DeserializeObject(filename As String)
      ' Create an instance of the XmlSerializer class.
      Dim mySerializer As XmlSerializer =  CreateOverrideSerializer()
      ' Reading the file requires a TextReader.
      Dim reader As TextReader = New StreamReader(filename)

      ' Deserialize and cast the object.
      Dim myGroup As Group  = _
      CType(mySerializer.Deserialize(reader), Group)

      Console.WriteLine(myGroup.GroupName)
   End Sub
End Class

[C#] 
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// The SoapType is overridden when the
// SerializeOverride  method is called.
[SoapType("SoapGroupType", "http://www.cohowinery.com")]
public class Group
{
   public string GroupName;
   public Employee[] Employees;
}

[SoapType("EmployeeType")]
public class Employee{
   public string Name;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeOriginal("SoapType.xml");
      test.SerializeOverride("SoapType2.xml");
      test.DeserializeObject("SoapType2.xml");
   }

   public void SerializeOriginal(string filename){
      // Create an instance of the XmlSerializer class that
      // can be used for serializing as a SOAP message.
     XmlTypeMapping mapp = 
      (new SoapReflectionImporter()).ImportTypeMapping(typeof(Group));
      XmlSerializer mySerializer =  
      new XmlSerializer(mapp);
      
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";
      Employee e1 = new Employee();
      e1.Name = "Pat";
      myGroup.Employees=new Employee[]{e1};

      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
       writer.Close();
   }


   public void SerializeOverride(string filename)
   {
      // Create an instance of the XmlSerializer class that
      // uses a SoapAttributeOverrides object.
      
      XmlSerializer mySerializer =  CreateOverrideSerializer();

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";
      Employee e1 = new Employee();
      e1.Name = "Pat";
      myGroup.Employees=new Employee[]{e1};

       // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
       writer.Close();
   }

   private XmlSerializer CreateOverrideSerializer()
   {
      // Create and return an XmlSerializer instance used to
      // override and create SOAP messages.
      SoapAttributeOverrides mySoapAttributeOverrides = 
          new SoapAttributeOverrides();
      SoapAttributes soapAtts = new SoapAttributes();

      // Override the SoapTypeAttribute.
      SoapTypeAttribute soapType = new SoapTypeAttribute();
      soapType.TypeName = "Team";
      soapType.IncludeInSchema = false;
      soapType.Namespace = "https://www.microsoft.com";
      soapAtts.SoapType = soapType;
      
      mySoapAttributeOverrides.Add(typeof(Group),soapAtts);

      // Create an XmlTypeMapping that is used to create an instance 
      // of the XmlSerializer. Then return the XmlSerializer object.
      XmlTypeMapping myMapping = (new SoapReflectionImporter(
      mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));
    
      XmlSerializer ser = new XmlSerializer(myMapping);
      return ser;
   }

   public void DeserializeObject(string filename){
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrideSerializer();
      // Reading the file requires a TextReader.
      TextReader reader = new StreamReader(filename);

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

      Console.WriteLine(myGroup.GroupName);
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

[SoapType(S"EmployeeType")]
__gc public class Employee
{
public:
   String* Name;
};

// The SoapType is overridden when the
// SerializeOverride  method is called.
[SoapType(S"SoapGroupType", S"http://www.cohowinery.com")]
__gc public class Group
{
public:
   String* GroupName;
   Employee * Employees[];
};

__gc public class Run
{
public:
   void SerializeOriginal(String* filename)
   {
      // Create an instance of the XmlSerializer class that
      // can be used for serializing as a SOAP message.
      XmlTypeMapping * mapp = 
         (new SoapReflectionImporter()) -> ImportTypeMapping(__typeof(Group));
      XmlSerializer* mySerializer = new XmlSerializer(mapp);

      // Writing the file requires a TextWriter.
      TextWriter* writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group* myGroup = new Group();

      // Set the Object* properties.
      myGroup -> GroupName = S".NET";
      Employee* e1 = new Employee();
      e1 -> Name = S"Pat";
      myGroup -> Employees = new Employee*[1];

      myGroup -> Employees[0] = e1;

      // Serialize the class, and close the TextWriter.
      mySerializer -> Serialize(writer, myGroup);
      writer -> Close();
   }

   void SerializeOverride(String* filename) 
   {
      // Create an instance of the XmlSerializer class that
      // uses a SoapAttributeOverrides Object*.

      XmlSerializer * mySerializer =  CreateOverrideSerializer();

      // Writing the file requires a TextWriter.
      TextWriter* writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group* myGroup = new Group();

      // Set the Object* properties.
      myGroup -> GroupName = S".NET";
      Employee* e1 = new Employee();
      e1 -> Name = S"Pat";
      myGroup -> Employees = new Employee*[5];

      myGroup -> Employees[0] = e1;

      // Serialize the class, and close the TextWriter.
      mySerializer -> Serialize(writer, myGroup);
      writer -> Close();
   }

   void DeserializeObject(String* filename) 
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer * mySerializer =  CreateOverrideSerializer();
      // Reading the file requires a TextReader.
      TextReader* reader = new StreamReader(filename);

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

      Console::WriteLine(myGroup -> GroupName);
   }

private:
   XmlSerializer * CreateOverrideSerializer() 
   {
      // Create and return an XmlSerializer instance used to
      //  and create SOAP messages.
      SoapAttributeOverrides* mySoapAttributeOverrides = new SoapAttributeOverrides();
      SoapAttributes* soapAtts = new SoapAttributes();

      // Override the SoapTypeAttribute.
      SoapTypeAttribute* soapType = new SoapTypeAttribute();
      soapType -> TypeName = S"Team";
      soapType -> IncludeInSchema = false;
      soapType -> Namespace = S"https://www.microsoft.com";
      soapAtts -> SoapType = soapType;

      mySoapAttributeOverrides -> Add(__typeof(Group), soapAtts);

      // Create an XmlTypeMapping that is used to create an instance 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping * myMapping = (new SoapReflectionImporter(mySoapAttributeOverrides)) -> ImportTypeMapping(__typeof(Group));

      XmlSerializer* ser = new XmlSerializer(myMapping);
      return ser;
   }
};

int main() 
{
   Run* test = new Run();
   test -> SerializeOriginal(S"SoapType.xml");
   test -> SerializeOverride(S"SoapType2.xml");
   test -> DeserializeObject(S"SoapType2.xml");
}

[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 ファミリ, .NET Compact Framework - Windows CE .NET

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

参照

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