SoapEnumAttribute Klasa

Definicja

Określa sposób XmlSerializer serializacji elementu członkowskiego wyliczenia.

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
Dziedziczenie
SoapEnumAttribute
Atrybuty

Przykłady

W poniższym przykładzie użyto klasy XmlSerializer , aby serializować klasę o nazwie Food , która zawiera wyliczenie o nazwie FoodType. Wyliczenie FoodType jest zastępowane przez utworzenie elementu SoapEnumAttribute dla każdego wyliczenia i ustawienie SoapEnum właściwości elementu SoapAttributes na SoapEnumAttributewartość . Element SoapAttributes jest dodawany do SoapAttributeOverrides elementu , który służy do tworzenia elementu 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

Uwagi

Klasa SoapEnumAttribute należy do rodziny atrybutów, które kontrolują sposób XmlSerializer serializacji lub deserializacji obiektu jako zakodowanego kodu SOAP XML. Wynikowy kod XML jest zgodny z sekcją 5 dokumentu World Wide Web Consortium Simple Object Access Protocol (SOAP) 1.1. Aby uzyskać pełną listę podobnych atrybutów, zobacz Atrybuty, które kontrolują zakodowaną serializacji PROTOKOŁU SOAP.

Aby serializować obiekt jako zakodowany komunikat SOAP, należy skonstruować XmlSerializer metodę XmlTypeMapping utworzoną przy ImportTypeMapping użyciu metody SoapReflectionImporter klasy .

Użyj metody SoapEnumAttribute , aby zmienić wyliczenie, które XmlSerializer generuje lub rozpoznaje (gdy serializuje lub deserializuje odpowiednio klasę). Jeśli na przykład wyliczenie zawiera element członkowski o nazwie One, ale wolisz, że dane wyjściowe XML mają nazwę Single, zastosuj SoapEnumAttribute element członkowski wyliczenia i ustaw Name właściwość na "Pojedynczy".

Wartość właściwości klasy można zastąpićName, tworząc wystąpienie SoapEnumAttribute klasy i przypisując ją do SoapEnum właściwości klasy SoapAttributes.SoapEnumAttribute Aby uzyskać szczegółowe informacje, zobacz SoapAttributeOverrides omówienie klasy.

Aby serializować obiekt jako zakodowany komunikat SOAP, należy skonstruować XmlSerializer metodę XmlTypeMapping utworzoną przy ImportTypeMapping użyciu metody SoapReflectionImporter klasy .

Uwaga

Możesz użyć słowa SoapEnum w kodzie zamiast dłuższego SoapEnumAttribute.

Aby uzyskać więcej informacji na temat używania atrybutów, zobacz Atrybuty.

Konstruktory

SoapEnumAttribute()

Inicjuje nowe wystąpienie klasy SoapEnumAttribute.

SoapEnumAttribute(String)

Inicjuje SoapEnumAttribute nowe wystąpienie klasy przy użyciu określonej nazwy elementu.

Właściwości

Name

Pobiera lub ustawia wartość wygenerowaną w dokumencie XML, gdy XmlSerializer serializuje wyliczenie lub wartość rozpoznaną podczas deserializacji elementu członkowskiego wyliczenia.

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego Attributeelementu .

(Odziedziczone po Attribute)

Metody

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

(Odziedziczone po Attribute)
GetHashCode()

Zwraca wartość skrótu dla tego wystąpienia.

(Odziedziczone po Attribute)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
IsDefaultAttribute()

W przypadku zastąpienia w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną klasy pochodnej.

(Odziedziczone po Attribute)
Match(Object)

Po przesłonięciu w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

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

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie dla obiektu, który może służyć do pobierania informacji o typie dla interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy