SoapTypeAttributeとは? わかりやすく解説

SoapTypeAttribute クラス

対象の型の SOAP 生成および SOAP 処理をカスタマイズします。このクラスは継承できません。

名前空間: System.Runtime.Remoting.Metadata
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct
 Or AttributeTargets.Enum Or AttributeTargets.Interface)>
 _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 SoapTypeAttribute
    Inherits SoapAttribute
Dim instance As SoapTypeAttribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Interface)]
 
[ComVisibleAttribute(true)] 
public sealed class SoapTypeAttribute : SoapAttribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Enum|AttributeTargets::Interface)]
 
[ComVisibleAttribute(true)] 
public ref class SoapTypeAttribute sealed :
 public SoapAttribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Interface)
 */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class SoapTypeAttribute extends
 SoapAttribute
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Interface)
 
ComVisibleAttribute(true) 
public final class SoapTypeAttribute extends
 SoapAttribute
解説解説
使用例使用例

SoapTypeAttribute を使用して、クラスに生成される SOAP をカスタマイズする方法のコード例を次に示します。SOAP は、SerializableAttribute クラスの例に示したコードによって生成できます。

<Serializable(), SoapTypeAttribute(XmlNamespace := "MyXmlNamespace")>
 Public Class TestSimpleObject
   
   Public member1 As Integer

   <SoapFieldAttribute(XmlElementName := "MyXmlElement")>
 Public member2 As String
   
   Public member3 As String
   Public member4 As Double

   ' A field that is not serialized.
   <NonSerialized()> Public member5 As
 String  


   Public Sub New()
      member1 = 11
      member2 = "hello"
      member3 = "hello"
      member4 = 3.14159265
      member5 = "hello world!"
   End Sub 'New

End Class 'TestSimpleObject
[Serializable()] 
[SoapTypeAttribute(XmlNamespace="MyXmlNamespace")]
public class TestSimpleObject  {

    public int member1;

    [SoapFieldAttribute(XmlElementName="MyXmlElement")] public
 string member2;

    public string member3;
    public double member4;

    // A field that is not serialized.
    [NonSerialized()] public string member5;
 
    
    public TestSimpleObject() {

        member1 = 11;
        member2 = "hello";
        member3 = "hello";
        member4 = 3.14159265;
        member5 = "hello world!";
    }
}
[Serializable]
[SoapTypeAttribute(XmlNamespace="MyXmlNamespace")]
public ref class TestSimpleObject
{
public:
   int member1;

   [SoapFieldAttribute(XmlElementName="MyXmlElement")] String^ member2;

   String^ member3;
   double member4;

   // A field that is not serialized.

   [NonSerialized] String^ member5;

   TestSimpleObject()
   {
      member1 = 11;
      member2 = "hello";
      member3 = "hello";
      member4 = 3.14159265;
      member5 = "hello world!";
   }
};
/** @attribute Serializable()
 */
/** @attribute SoapTypeAttribute(XmlNamespace = "MyXmlNamespace")
 */
public class TestSimpleObject
{
    public int member1;

    /** @attribute SoapFieldAttribute(XmlElementName = "MyXmlElement")
     */
    public String member2;
    public String member3;
    public double member4;

    // A field that is not serialized.
    /** @attribute NonSerialized()
     */
    public String member5;

    public TestSimpleObject()
    {
        member1 = 11;
        member2 = "hello";
        member3 = "hello";
        member4 = 3.14159265;
        member5 = "hello world!";
    } //TestSimpleObject
} //TestSimpleObject
継承階層継承階層
System.Object
   System.Attribute
     System.Runtime.Remoting.Metadata.SoapAttribute
      System.Runtime.Remoting.Metadata.SoapTypeAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SoapTypeAttribute メンバ
System.Runtime.Remoting.Metadata 名前空間

SoapTypeAttribute クラス

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

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct
 Or AttributeTargets.Enum Or AttributeTargets.Interface)>
 _
Public Class SoapTypeAttribute
    Inherits Attribute
Dim instance As SoapTypeAttribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Interface)]
 
public class SoapTypeAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Enum|AttributeTargets::Interface)]
 
public ref class SoapTypeAttribute : public
 Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Interface)
 */ 
public class SoapTypeAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Interface)
 
public class SoapTypeAttribute extends
 Attribute
解説解説

SoapTypeAttribute クラスは、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) ファイルを作成します。

使用例使用例

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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 = "http://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)

      ' Create an XML text reader.
      Dim xmlReader As XmlTextReader = New
 XmlTextReader(reader)
      xmlReader.ReadStartElement()

      ' Deserialize and cast the object.
      Dim myGroup As Group = CType(mySerializer.Deserialize(xmlReader),
 Group)
      xmlReader.ReadEndElement()
      Console.WriteLine("The GroupName is " + myGroup.GroupName)
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml
 " + _
        "files for the generated XML.")

      ' Close the readers.
      xmlReader.Close()
      reader.Close()
   End Sub
End Class
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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 = "http://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);

      // Create an XML text reader.
      XmlTextReader xmlReader = new XmlTextReader(reader);
      xmlReader.ReadStartElement();

      // Deserialize and cast the object.
      Group myGroup = (Group) mySerializer.Deserialize(xmlReader);
      xmlReader.ReadEndElement();
      Console.WriteLine("The GroupName is " + myGroup.GroupName);
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml " +
        "files for the generated XML.");

      // Close the readers.
      xmlReader.Close();
      reader.Close();
   }
}
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

[SoapType("EmployeeType")]
public ref class Employee
{
public:
   String^ Name;
};


// The SoapType is overridden when the
// SerializeOverride  method is called.

[SoapType("SoapGroupType","http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;
   array<Employee^>^Employees;
};

public ref 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 = (gcnew SoapReflectionImporter)->ImportTypeMapping(
 Group::typeid );
      XmlSerializer^ mySerializer = gcnew XmlSerializer( mapp );

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

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      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 = gcnew StreamWriter( filename );

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      writer->Close();
   }

   void DeserializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer^ mySerializer = CreateOverrideSerializer();

      // Reading the file requires a TextReader.
      TextReader^ reader = gcnew StreamReader( filename );

      // Create an XML text reader.
      XmlTextReader^ xmlReader = gcnew XmlTextReader( reader );
      xmlReader->ReadStartElement();

      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( xmlReader
 ));
      xmlReader->ReadEndElement();
      Console::WriteLine( "The GroupName is {0}", myGroup->GroupName
 );
      Console::WriteLine( "Look at the SoapType.xml and SoapType2.xml "
      "files for the generated XML." );

      // Close the readers.
      xmlReader->Close();
      reader->Close();
   }

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

      // Override the SoapTypeAttribute.
      SoapTypeAttribute^ soapType = gcnew SoapTypeAttribute;
      soapType->TypeName = "Team";
      soapType->IncludeInSchema = false;
      soapType->Namespace = "http://www.microsoft.com";
      soapAtts->SoapType = soapType;
      mySoapAttributeOverrides->Add( Group::typeid, soapAtts );

      // Create an XmlTypeMapping that is used to create an instance
 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping^ myMapping = (gcnew SoapReflectionImporter( mySoapAttributeOverrides
 ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( myMapping );
      return ser;
   }
};

int main()
{
   Run^ test = gcnew Run;
   test->SerializeOriginal( "SoapType.xml" );
   test->SerializeOverride( "SoapType2.xml" );
   test->DeserializeObject( "SoapType2.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// The SoapType is overridden when the
// SerializeOverride  method is called.

/** @attribute SoapType("SoapGroupType", "http://www.cohowinery.com")
 */
public class Group
{
    public String groupName;
    public Employee employees[];
} //Group

/** @attribute SoapType("EmployeeType")
 */
public class Employee
{
    public String name;
} //Employee

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

    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(Group.class.ToType());
        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();
    } //SerializeOriginal

    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();
    } //SerializeOverride

    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.set_TypeName("Team");
        soapType.set_IncludeInSchema(false);
        soapType.set_Namespace("http://www.microsoft.com");
        soapAtts.set_SoapType(soapType);

        mySoapAttributeOverrides.Add(Group.class.ToType(), 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(Group.class.ToType());

        XmlSerializer ser = new XmlSerializer(myMapping);
        return ser;
    } //CreateOverrideSerializer

    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);
    } //DeserializeObject
} //Run
継承階層継承階層
System.Object
   System.Attribute
    System.Xml.Serialization.SoapTypeAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SoapTypeAttribute メンバ
System.Xml.Serialization 名前空間

SoapTypeAttribute コンストラクタ ()

SoapTypeAttribute クラスの新しいインスタンスを初期化します。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Dim instance As New SoapTypeAttribute
public SoapTypeAttribute ()
public:
SoapTypeAttribute ()
public SoapTypeAttribute ()
public function SoapTypeAttribute ()
解説解説

型のシリアル化をオーバーライドするときには、SoapTypeAttribute を作成します。このオブジェクトを SoapAttributes の SoapType プロパティに割り当て、その SoapAttributes を SoapAttributeOverrides に追加します。SOAP シリアル化のオーバーライドの詳細については、SoapAttributeOverrides クラスの概要を参照してください。

使用例使用例

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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 = "http://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)

      ' Create an XML text reader.
      Dim xmlReader As XmlTextReader = New
 XmlTextReader(reader)
      xmlReader.ReadStartElement()

      ' Deserialize and cast the object.
      Dim myGroup As Group = CType(mySerializer.Deserialize(xmlReader),
 Group)
      xmlReader.ReadEndElement()
      Console.WriteLine("The GroupName is " + myGroup.GroupName)
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml
 " + _
        "files for the generated XML.")

      ' Close the readers.
      xmlReader.Close()
      reader.Close()
   End Sub
End Class
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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 = "http://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);

      // Create an XML text reader.
      XmlTextReader xmlReader = new XmlTextReader(reader);
      xmlReader.ReadStartElement();

      // Deserialize and cast the object.
      Group myGroup = (Group) mySerializer.Deserialize(xmlReader);
      xmlReader.ReadEndElement();
      Console.WriteLine("The GroupName is " + myGroup.GroupName);
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml " +
        "files for the generated XML.");

      // Close the readers.
      xmlReader.Close();
      reader.Close();
   }
}
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

[SoapType("EmployeeType")]
public ref class Employee
{
public:
   String^ Name;
};


// The SoapType is overridden when the
// SerializeOverride  method is called.

[SoapType("SoapGroupType","http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;
   array<Employee^>^Employees;
};

public ref 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 = (gcnew SoapReflectionImporter)->ImportTypeMapping(
 Group::typeid );
      XmlSerializer^ mySerializer = gcnew XmlSerializer( mapp );

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

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      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 = gcnew StreamWriter( filename );

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      writer->Close();
   }

   void DeserializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer^ mySerializer = CreateOverrideSerializer();

      // Reading the file requires a TextReader.
      TextReader^ reader = gcnew StreamReader( filename );

      // Create an XML text reader.
      XmlTextReader^ xmlReader = gcnew XmlTextReader( reader );
      xmlReader->ReadStartElement();

      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( xmlReader
 ));
      xmlReader->ReadEndElement();
      Console::WriteLine( "The GroupName is {0}", myGroup->GroupName
 );
      Console::WriteLine( "Look at the SoapType.xml and SoapType2.xml "
      "files for the generated XML." );

      // Close the readers.
      xmlReader->Close();
      reader->Close();
   }

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

      // Override the SoapTypeAttribute.
      SoapTypeAttribute^ soapType = gcnew SoapTypeAttribute;
      soapType->TypeName = "Team";
      soapType->IncludeInSchema = false;
      soapType->Namespace = "http://www.microsoft.com";
      soapAtts->SoapType = soapType;
      mySoapAttributeOverrides->Add( Group::typeid, soapAtts );

      // Create an XmlTypeMapping that is used to create an instance
 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping^ myMapping = (gcnew SoapReflectionImporter( mySoapAttributeOverrides
 ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( myMapping );
      return ser;
   }
};

int main()
{
   Run^ test = gcnew Run;
   test->SerializeOriginal( "SoapType.xml" );
   test->SerializeOverride( "SoapType2.xml" );
   test->DeserializeObject( "SoapType2.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// The SoapType is overridden when the
// SerializeOverride  method is called.

/** @attribute SoapType("SoapGroupType", "http://www.cohowinery.com")
 */
public class Group
{
    public String groupName;
    public Employee employees[];
} //Group

/** @attribute SoapType("EmployeeType")
 */
public class Employee
{
    public String name;
} //Employee

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

    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(Group.class.ToType());
        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();
    } //SerializeOriginal

    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();
    } //SerializeOverride

    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.set_TypeName("Team");
        soapType.set_IncludeInSchema(false);
        soapType.set_Namespace("http://www.microsoft.com");
        soapAtts.set_SoapType(soapType);

        mySoapAttributeOverrides.Add(Group.class.ToType(), 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(Group.class.ToType());

        XmlSerializer ser = new XmlSerializer(myMapping);
        return ser;
    } //CreateOverrideSerializer

    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);
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SoapTypeAttribute クラス
SoapTypeAttribute メンバ
System.Xml.Serialization 名前空間

SoapTypeAttribute コンストラクタ (String)

SoapTypeAttribute クラスの新しいインスタンスを初期化し、XML 型の名前を指定します。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

public SoapTypeAttribute (
    string typeName
)
public:
SoapTypeAttribute (
    String^ typeName
)
public SoapTypeAttribute (
    String typeName
)
public function SoapTypeAttribute (
    typeName : String
)

パラメータ

typeName

XmlSerializer がクラス インスタンスをシリアル化する場合に生成する (およびクラス インスタンスを逆シリアル化する場合に認識する) XML 型の名前。

解説解説

型のシリアル化をオーバーライドするときには、SoapTypeAttribute を作成します。このオブジェクトを SoapAttributes の SoapType プロパティに割り当て、その SoapAttributes を SoapAttributeOverrides に追加します。SOAP シリアル化のオーバーライドの詳細については、SoapAttributeOverrides クラスの概要を参照してください。

使用例使用例

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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 = "http://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)

      ' Create an XML text reader.
      Dim xmlReader As XmlTextReader = New
 XmlTextReader(reader)
      xmlReader.ReadStartElement()

      ' Deserialize and cast the object.
      Dim myGroup As Group = CType(mySerializer.Deserialize(xmlReader),
 Group)
      xmlReader.ReadEndElement()
      Console.WriteLine("The GroupName is " + myGroup.GroupName)
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml
 " + _
        "files for the generated XML.")

      ' Close the readers.
      xmlReader.Close()
      reader.Close()
   End Sub
End Class
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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 = "http://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);

      // Create an XML text reader.
      XmlTextReader xmlReader = new XmlTextReader(reader);
      xmlReader.ReadStartElement();

      // Deserialize and cast the object.
      Group myGroup = (Group) mySerializer.Deserialize(xmlReader);
      xmlReader.ReadEndElement();
      Console.WriteLine("The GroupName is " + myGroup.GroupName);
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml " +
        "files for the generated XML.");

      // Close the readers.
      xmlReader.Close();
      reader.Close();
   }
}
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

[SoapType("EmployeeType")]
public ref class Employee
{
public:
   String^ Name;
};


// The SoapType is overridden when the
// SerializeOverride  method is called.

[SoapType("SoapGroupType","http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;
   array<Employee^>^Employees;
};

public ref 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 = (gcnew SoapReflectionImporter)->ImportTypeMapping(
 Group::typeid );
      XmlSerializer^ mySerializer = gcnew XmlSerializer( mapp );

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

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      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 = gcnew StreamWriter( filename );

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      writer->Close();
   }

   void DeserializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer^ mySerializer = CreateOverrideSerializer();

      // Reading the file requires a TextReader.
      TextReader^ reader = gcnew StreamReader( filename );

      // Create an XML text reader.
      XmlTextReader^ xmlReader = gcnew XmlTextReader( reader );
      xmlReader->ReadStartElement();

      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( xmlReader
 ));
      xmlReader->ReadEndElement();
      Console::WriteLine( "The GroupName is {0}", myGroup->GroupName
 );
      Console::WriteLine( "Look at the SoapType.xml and SoapType2.xml "
      "files for the generated XML." );

      // Close the readers.
      xmlReader->Close();
      reader->Close();
   }

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

      // Override the SoapTypeAttribute.
      SoapTypeAttribute^ soapType = gcnew SoapTypeAttribute;
      soapType->TypeName = "Team";
      soapType->IncludeInSchema = false;
      soapType->Namespace = "http://www.microsoft.com";
      soapAtts->SoapType = soapType;
      mySoapAttributeOverrides->Add( Group::typeid, soapAtts );

      // Create an XmlTypeMapping that is used to create an instance
 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping^ myMapping = (gcnew SoapReflectionImporter( mySoapAttributeOverrides
 ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( myMapping );
      return ser;
   }
};

int main()
{
   Run^ test = gcnew Run;
   test->SerializeOriginal( "SoapType.xml" );
   test->SerializeOverride( "SoapType2.xml" );
   test->DeserializeObject( "SoapType2.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// The SoapType is overridden when the
// SerializeOverride  method is called.

/** @attribute SoapType("SoapGroupType", "http://www.cohowinery.com")
 */
public class Group
{
    public String groupName;
    public Employee employees[];
} //Group

/** @attribute SoapType("EmployeeType")
 */
public class Employee
{
    public String name;
} //Employee

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

    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(Group.class.ToType());
        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();
    } //SerializeOriginal

    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();
    } //SerializeOverride

    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.set_TypeName("Team");
        soapType.set_IncludeInSchema(false);
        soapType.set_Namespace("http://www.microsoft.com");
        soapAtts.set_SoapType(soapType);

        mySoapAttributeOverrides.Add(Group.class.ToType(), 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(Group.class.ToType());

        XmlSerializer ser = new XmlSerializer(myMapping);
        return ser;
    } //CreateOverrideSerializer

    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);
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SoapTypeAttribute クラス
SoapTypeAttribute メンバ
System.Xml.Serialization 名前空間

SoapTypeAttribute コンストラクタ (String, String)

SoapTypeAttribute クラスの新しいインスタンスを初期化し、型の名前および型の XML 名前空間を指定します。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

解説解説

型のシリアル化をオーバーライドするときには、SoapTypeAttribute を作成します。このオブジェクトを SoapAttributes の SoapType プロパティに割り当て、その SoapAttributes を SoapAttributeOverrides に追加します。SOAP シリアル化のオーバーライドの詳細については、SoapAttributeOverrides クラスの概要を参照してください。

複数の型に対して Namespace 値を設定 (つまり、この属性を、それぞれの Namespace 値が異なる複数のクラスに適用) した場合、XML スキーマ定義ツール (Xsd.exe) は型ごとに個別のスキーマ ファイル (.xsd) を生成します。これは、型ごとに異なる名前空間を設定することによって各型が他の型と区別され、したがって、それぞれの型を個別のエンティティとして記述する必要が生じるためです。

使用例使用例

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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 XML text writer.
      Dim xmlWriter As XmlTextWriter = New
 XmlTextWriter(writer)
      xmlWriter.Formatting = Formatting.Indented
      xmlWriter.Indentation = 2

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

      ' Write the root element.
      xmlWriter.WriteStartElement("root")

      ' Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup)

      ' Close the root tag.
      xmlWriter.WriteEndElement()

      ' Close the XmlWriter.
      xmlWriter.Close()

      ' Close the TextWriter.
      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 = "http://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)

      ' Create an XML text reader.
      Dim xmlReader As XmlTextReader = New
 XmlTextReader(reader)
      xmlReader.ReadStartElement()

      ' Deserialize and cast the object.
      Dim myGroup As Group = CType(mySerializer.Deserialize(xmlReader),
 Group)
      xmlReader.ReadEndElement()
      Console.WriteLine("The GroupName is " + myGroup.GroupName)
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml
 " + _
        "files for the generated XML.")

      ' Close the readers.
      xmlReader.Close()
      reader.Close()
   End Sub
End Class
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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

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

      // Write the root element.
      xmlWriter.WriteStartElement("root");

      // Serialize the class.
      mySerializer.Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter.WriteEndElement();

      // Close the XmlWriter.
      xmlWriter.Close();

      // Close the TextWriter.
      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 = "http://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);

      // Create an XML text reader.
      XmlTextReader xmlReader = new XmlTextReader(reader);
      xmlReader.ReadStartElement();

      // Deserialize and cast the object.
      Group myGroup = (Group) mySerializer.Deserialize(xmlReader);
      xmlReader.ReadEndElement();
      Console.WriteLine("The GroupName is " + myGroup.GroupName);
      Console.WriteLine("Look at the SoapType.xml and SoapType2.xml " +
        "files for the generated XML.");

      // Close the readers.
      xmlReader.Close();
      reader.Close();
   }
}
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

[SoapType("EmployeeType")]
public ref class Employee
{
public:
   String^ Name;
};


// The SoapType is overridden when the
// SerializeOverride  method is called.

[SoapType("SoapGroupType","http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;
   array<Employee^>^Employees;
};

public ref 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 = (gcnew SoapReflectionImporter)->ImportTypeMapping(
 Group::typeid );
      XmlSerializer^ mySerializer = gcnew XmlSerializer( mapp );

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

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      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 = gcnew StreamWriter( filename );

      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = e1;

      // Write the root element.
      xmlWriter->WriteStartElement( "root" );

      // Serialize the class.
      mySerializer->Serialize( xmlWriter, myGroup );

      // Close the root tag.
      xmlWriter->WriteEndElement();

      // Close the XmlWriter.
      xmlWriter->Close();

      // Close the TextWriter.
      writer->Close();
   }

   void DeserializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer^ mySerializer = CreateOverrideSerializer();

      // Reading the file requires a TextReader.
      TextReader^ reader = gcnew StreamReader( filename );

      // Create an XML text reader.
      XmlTextReader^ xmlReader = gcnew XmlTextReader( reader );
      xmlReader->ReadStartElement();

      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( xmlReader
 ));
      xmlReader->ReadEndElement();
      Console::WriteLine( "The GroupName is {0}", myGroup->GroupName
 );
      Console::WriteLine( "Look at the SoapType.xml and SoapType2.xml "
      "files for the generated XML." );

      // Close the readers.
      xmlReader->Close();
      reader->Close();
   }

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

      // Override the SoapTypeAttribute.
      SoapTypeAttribute^ soapType = gcnew SoapTypeAttribute;
      soapType->TypeName = "Team";
      soapType->IncludeInSchema = false;
      soapType->Namespace = "http://www.microsoft.com";
      soapAtts->SoapType = soapType;
      mySoapAttributeOverrides->Add( Group::typeid, soapAtts );

      // Create an XmlTypeMapping that is used to create an instance
 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping^ myMapping = (gcnew SoapReflectionImporter( mySoapAttributeOverrides
 ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( myMapping );
      return ser;
   }
};

int main()
{
   Run^ test = gcnew Run;
   test->SerializeOriginal( "SoapType.xml" );
   test->SerializeOverride( "SoapType2.xml" );
   test->DeserializeObject( "SoapType2.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// The SoapType is overridden when the
// SerializeOverride  method is called.

/** @attribute SoapType("SoapGroupType", "http://www.cohowinery.com")
 */
public class Group
{
    public String groupName;
    public Employee employees[];
} //Group

/** @attribute SoapType("EmployeeType")
 */
public class Employee
{
    public String name;
} //Employee

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

    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(Group.class.ToType());
        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();
    } //SerializeOriginal

    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();
    } //SerializeOverride

    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.set_TypeName("Team");
        soapType.set_IncludeInSchema(false);
        soapType.set_Namespace("http://www.microsoft.com");
        soapAtts.set_SoapType(soapType);

        mySoapAttributeOverrides.Add(Group.class.ToType(), 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(Group.class.ToType());

        XmlSerializer ser = new XmlSerializer(myMapping);
        return ser;
    } //CreateOverrideSerializer

    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);
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SoapTypeAttribute クラス
SoapTypeAttribute メンバ
System.Xml.Serialization 名前空間

SoapTypeAttribute コンストラクタ


SoapTypeAttribute コンストラクタ

SoapTypeAttribute クラスの新しいインスタンスを初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
SoapTypeAttribute () SoapTypeAttribute クラスの新しいインスタンスを初期化します。

.NET Compact Framework によってサポートされています。

SoapTypeAttribute (String) SoapTypeAttribute クラスの新しいインスタンスを初期化し、XML 型の名前を指定します。

.NET Compact Framework によってサポートされています。

SoapTypeAttribute (String, String) SoapTypeAttribute クラスの新しいインスタンスを初期化し、型の名前および型の XML 名前空間を指定します。

.NET Compact Framework によってサポートされています。

参照参照

関連項目

SoapTypeAttribute クラス
SoapTypeAttribute メンバ
System.Xml.Serialization 名前空間

SoapTypeAttribute プロパティ


SoapTypeAttribute プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ IncludeInSchema SOAP エンコード済みの XML スキーマ ドキュメントに型を含めるかどうかを示す値を取得または設定します。
パブリック プロパティ .NET Compact Framework によるサポート TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。 ( Attribute から継承されます。)
パブリック プロパティ TypeName XML 型の名前を取得または設定します。
参照参照

関連項目

SoapTypeAttribute クラス
System.Xml.Serialization 名前空間

SoapTypeAttribute メソッド


パブリック メソッドパブリック メソッド

  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
参照参照

関連項目

SoapTypeAttribute クラス
System.Runtime.Remoting.Metadata 名前空間

SoapTypeAttribute メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SoapTypeAttribute クラス
System.Xml.Serialization 名前空間

SoapTypeAttribute メンバ

対象の型の SOAP 生成および SOAP 処理をカスタマイズします。このクラスは継承できません。

SoapTypeAttribute データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド SoapTypeAttribute  
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
参照参照

関連項目

SoapTypeAttribute クラス
System.Runtime.Remoting.Metadata 名前空間

SoapTypeAttribute メンバ

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

SoapTypeAttribute データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド SoapTypeAttribute オーバーロードされます。 SoapTypeAttribute クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ IncludeInSchema SOAP エンコード済みの XML スキーマ ドキュメントに型を含めるかどうかを示す値を取得または設定します。
パブリック プロパティ .NET Compact Framework によるサポート TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。(Attribute から継承されます。)
パブリック プロパティ TypeName XML 型の名前を取得または設定します。
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SoapTypeAttribute クラス
System.Xml.Serialization 名前空間



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「SoapTypeAttribute」の関連用語

SoapTypeAttributeのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



SoapTypeAttributeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS