SoapEnumAttribute 類別

定義

控制 XmlSerializer 序列化列舉型別 (Enumeration) 成員的方式。

public ref class SoapEnumAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field)]
public class SoapEnumAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field)>]
type SoapEnumAttribute = class
    inherit Attribute
Public Class SoapEnumAttribute
Inherits Attribute
繼承
SoapEnumAttribute
屬性

範例

下列範例會使用 XmlSerializer 序列化名為 Food 的類別,其中包含名為 FoodType 的列舉。 藉 FoodType 由為每個列舉建立 , SoapEnumAttribute 並將 的 SoapAttributes 屬性設定 SoapEnumSoapEnumAttribute ,以覆寫列舉。 會 SoapAttributes 加入至 SoapAttributeOverrides ,用來建立 XmlSerializer

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public enum class GroupType
{
   // Use the SoapEnumAttribute to instruct the XmlSerializer
   // to generate Small and Large instead of A and B.
   [SoapEnum("Small")]
   A,
   [SoapEnum("Large")]
   B
};

public ref class Group
{
public:
   String^ GroupName;
   GroupType Grouptype;
};

public ref class Run
{
public:
   void SerializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer Class.
      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 instance of the Class that will be serialized.
      Group^ myGroup = gcnew Group;

      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      myGroup->Grouptype = GroupType::A;

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

   void SerializeOverride( String^ fileName )
   {
      SoapAttributeOverrides^ soapOver = gcnew SoapAttributeOverrides;
      SoapAttributes^ SoapAtts = gcnew SoapAttributes;

      // Add a SoapEnumAttribute for the GroupType::A enumerator.       
      // Instead of 'A'  it will be S"West".
      SoapEnumAttribute^ soapEnum = gcnew SoapEnumAttribute( "West" );

      // Override the S"A" enumerator.
      SoapAtts->GroupType::SoapEnum = soapEnum;
      soapOver->Add( GroupType::typeid, "A", SoapAtts );

      // Add another SoapEnumAttribute for the GroupType::B enumerator.
      // Instead of //B// it will be S"East".
      SoapAtts = gcnew SoapAttributes;
      soapEnum = gcnew SoapEnumAttribute;
      soapEnum->Name = "East";
      SoapAtts->GroupType::SoapEnum = soapEnum;
      soapOver->Add( GroupType::typeid, "B", SoapAtts );

      // Create an XmlSerializer used for overriding.
      XmlTypeMapping^ map = (gcnew SoapReflectionImporter( soapOver ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( map );
      Group^ myGroup = gcnew Group;
      myGroup->GroupName = ".NET";
      myGroup->Grouptype = GroupType::B;

      // Writing the file requires a TextWriter.
      TextWriter^ writer = gcnew StreamWriter( fileName );
      ser->Serialize( writer, myGroup );
      writer->Close();
   }
};

int main()
{
   Run^ test = gcnew Run;
   test->SerializeObject( "SoapEnum.xml" );
   test->SerializeOverride( "SoapOverride.xml" );
   Console::WriteLine( "Fininished writing two files" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Group{
   public string GroupName;
   public GroupType Grouptype;
}

public enum GroupType{
   // Use the SoapEnumAttribute to instruct the XmlSerializer
   // to generate Small and Large instead of A and B.
   [SoapEnum("Small")]
   A,
   [SoapEnum("Large")]
   B
}

public class Run {
   static void Main(){
      Run test= new Run();
      test.SerializeObject("SoapEnum.xml");
      test.SerializeOverride("SoapOverride.xml");
      Console.WriteLine("Fininished writing two files");
   }

     private void SerializeObject(string filename){
      // Create an instance of the XmlSerializer Class.
      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";
      myGroup.Grouptype= GroupType.A;

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

   private void SerializeOverride(string fileName){
      SoapAttributeOverrides soapOver = new SoapAttributeOverrides();
      SoapAttributes SoapAtts = new SoapAttributes();

      // Add a SoapEnumAttribute for the GroupType.A enumerator.
      // Instead of 'A'  it will be "West".
      SoapEnumAttribute soapEnum = new SoapEnumAttribute("West");
      // Override the "A" enumerator.
      SoapAtts.SoapEnum = soapEnum;
      soapOver.Add(typeof(GroupType), "A", SoapAtts);

      // Add another SoapEnumAttribute for the GroupType.B enumerator.
      // Instead of //B// it will be "East".
      SoapAtts= new SoapAttributes();
      soapEnum = new SoapEnumAttribute();
      soapEnum.Name = "East";
      SoapAtts.SoapEnum = soapEnum;
      soapOver.Add(typeof(GroupType), "B", SoapAtts);

      // Create an XmlSerializer used for overriding.
      XmlTypeMapping map =
      new SoapReflectionImporter(soapOver).
      ImportTypeMapping(typeof(Group));
      XmlSerializer ser = new XmlSerializer(map);
      Group myGroup = new Group();
      myGroup.GroupName = ".NET";
      myGroup.Grouptype = GroupType.B;
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(fileName);
      ser.Serialize(writer, myGroup);
      writer.Close();
    }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Class Group
   Public GroupName As String 
   Public Grouptype As GroupType 
End Class

Public enum GroupType
' Use the SoapEnumAttribute to instruct the XmlSerializer
' to generate Small and Large instead of A and B.
   <SoapEnum("Small")> _
   A
   <SoapEnum("Large")> _
   B
End enum
 
Public Class Run
   Public Shared Sub Main()
      Dim test As Run = new Run()
      test.SerializeObject("SoapEnum.xml")
      test.SerializeOverride("SoapOverride.xml")
      Console.WriteLine("Fininished writing two files")
   End Sub

   Private Shared Sub SerializeObject(filename As string)
      ' Create an instance of the XmlSerializer Class.
      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"
      myGroup.Grouptype= GroupType.A

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

   Private  Sub SerializeOverride(fileName As String)
      Dim soapOver As SoapAttributeOverrides = new SoapAttributeOverrides()
      Dim SoapAtts As SoapAttributes = new SoapAttributes()

      ' Add a SoapEnumAttribute for the GroupType.A enumerator. Instead
      ' of 'A' it will be "West".
      Dim soapEnum As SoapEnumAttribute = new SoapEnumAttribute("West")
      ' Override the "A" enumerator.
      SoapAtts.SoapEnum = soapEnum
      soapOver.Add(GetType(GroupType), "A", SoapAtts)

      ' Add another SoapEnumAttribute for the GroupType.B enumerator.
      ' Instead of 'B' it will be "East".
      SoapAtts= New SoapAttributes()
      soapEnum = new SoapEnumAttribute()
      soapEnum.Name = "East"
      SoapAtts.SoapEnum = soapEnum
      soapOver.Add(GetType(GroupType), "B", SoapAtts)

      ' Create an XmlSerializer used for overriding.
      Dim map As XmlTypeMapping = New SoapReflectionImporter _
      (soapOver).ImportTypeMapping(GetType(Group))
      Dim ser As XmlSerializer = New XmlSerializer(map)
      Dim myGroup As Group = New Group()
      myGroup.GroupName = ".NET"
      myGroup.Grouptype = GroupType.B
      ' Writing the file requires a TextWriter.
      Dim writer As TextWriter = New StreamWriter(fileName)
      ser.Serialize(writer, myGroup)
      writer.Close

   End Sub
End Class

備註

類別 SoapEnumAttribute 屬於一系列屬性,可控制如何將 XmlSerializer 物件序列化或還原序列化為編碼的 SOAP XML。 產生的 XML 符合 World Wide Web Consortium 檔 Simple Object Access Protocol (SOAP) 1.1的第 5 節。 如需類似屬性的完整清單,請參閱 控制編碼 SOAP 序列化的屬性

若要將物件序列化為編碼的 SOAP 訊息,您必須使用以 XmlTypeMapping ImportTypeMapping 類別的 方法建立的 SoapReflectionImporter 來建構 XmlSerializer

使用 來 SoapEnumAttribute 變更 在分別序列化或還原序列化) 類別時,產生或辨識 (的列舉 XmlSerializer 。 例如,如果列舉包含名為 One 的成員,但您偏好將 XML 輸出命名 Single 為 ,請將 套用 SoapEnumAttribute 至列舉成員,並將 屬性設定 Name 為 「Single」。

您可以建立 類別的 SoapEnumAttribute 實例,並將它指派給 SoapEnum 的 屬性,以覆寫 NameSoapAttributes 屬性值 SoapEnumAttribute 。 如需詳細資訊,請參閱類別概 SoapAttributeOverrides 觀。

若要將物件序列化為編碼的 SOAP 訊息,您必須使用以 XmlTypeMapping ImportTypeMapping 類別的 方法建立的 SoapReflectionImporter 來建構 XmlSerializer

注意

您可以在程式碼中使用這個字, SoapEnum 而不是較長 SoapEnumAttribute 的 。

如需使用屬性的詳細資訊,請參閱 屬性

建構函式

SoapEnumAttribute()

初始化 SoapEnumAttribute 類別的新執行個體。

SoapEnumAttribute(String)

使用指定的項目名稱,初始化 SoapEnumAttribute 類別的新執行個體。

屬性

Name

取得或設定當 XmlSerializer 序列化列舉型別時,在 XML 文件中所產生的值,或是當它還原序列化列舉型別成員時所識別的值。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於