Export (0) Print
Expand All

SoapEnumAttribute Class

Controls how the XmlSerializer serializes an enumeration member.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

[AttributeUsageAttribute(AttributeTargets.Field)]
public class SoapEnumAttribute : Attribute

The SoapEnumAttribute class belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object as encoded SOAP XML. The resulting XML conforms to section 5 of the World Wide Web Consortium (www.w3.org) document "Simple Object Access Protocol (SOAP) 1.1". For a complete list of similar attributes, see [<topic://cpconAttributesThatControlSOAPEncodedSerialization>].

To serialize an object as an encoded SOAP message, you must construct the XmlSerializer using an XmlTypeMapping created with the ImportTypeMapping method of the SoapReflectionImporter class.

Use the SoapEnumAttribute to change the enumeration that the XmlSerializer generates or recognizes (when it serializes or deserializes a class, respectively). For example, if an enumeration contains a member named One, but you prefer that the XML output be named Single, apply the SoapEnumAttribute to the enumeration member and set the Name property to "Single".

You can override the Name property value of a SoapEnumAttribute by creating an instance of the SoapEnumAttribute class and assigning it to the SoapEnum property of a SoapAttributes. For details, see the SoapAttributeOverrides class overview.

To serialize an object as an encoded SOAP message, you must construct the XmlSerializer using an XmlTypeMapping created with the ImportTypeMapping method of the SoapReflectionImporter class.

NoteNote:

You can use the word SoapEnum in your code instead of the longer SoapEnumAttribute.

For more information about using attributes, see Extending Metadata Using Attributes.

The following example uses the XmlSerializer to serialize a class named Food that includes an enumeration named FoodType. The FoodType enumeration is overridden by creating a SoapEnumAttribute for each enumeration and setting the SoapEnum property of a SoapAttributes to the SoapEnumAttribute. The SoapAttributes is added to a SoapAttributeOverrides that is used to create an XmlSerializer.

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();
   	}
}
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

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

__gc public class Group
{
public:
   String* GroupName;
   GroupType Grouptype;
};

__gc public class Run 
{
public:
   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 = S".NET";
      myGroup -> Grouptype= GroupType::A;

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

   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 S"West".
      SoapEnumAttribute* soapEnum = new SoapEnumAttribute(S"West");

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

      // Add another SoapEnumAttribute for the GroupType::B enumerator.
      // Instead of //B// it will be S"East".
      SoapAtts = new SoapAttributes();
      soapEnum = new SoapEnumAttribute();
      soapEnum -> Name = S"East";
      SoapAtts -> SoapEnum = soapEnum;
      soapOver -> Add(__typeof(GroupType), S"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 = S".NET";
      myGroup -> Grouptype = GroupType::B;
      // Writing the file requires a TextWriter.
      TextWriter* writer = new StreamWriter(fileName);
      ser -> Serialize(writer, myGroup);
      writer -> Close();
   }
};

int main() 
{
   Run* test = new Run();
   test -> SerializeObject(S"SoapEnum.xml");
   test -> SerializeOverride(S"SoapOverride.xml");
   Console::WriteLine(S"Fininished writing two files");
}

System.Object
  System.Attribute
    System.Xml.Serialization.SoapEnumAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft