Export (0) Print
Expand All

SoapAttributes.SoapEnum Property

Gets or sets an object that specifies how the XmlSerializer serializes a SOAP enumeration.

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

Public Property SoapEnum As SoapEnumAttribute

Property Value

Type: System.Xml.Serialization.SoapEnumAttribute
An object that specifies how the XmlSerializer serializes an enumeration member.

The SoapEnumAttribute is used to control the serialization of an enumeration member. Set the SoapEnum property to a new SoapEnumAttribute to override the serialization of such a member.

For more information, see the SoapAttributeOverrides class overview.

The following example serializes two classes named Food and FoodType. The FoodType class contains two enumerations that are overridden, and for each enumeration, the example creates a SoapEnumAttribute that it assigns to the SoapEnum property of a SoapAttributes. The example then adds the SoapAttributes to a SoapAttributeOverrides, which is used to create an XmlSerializer.

Imports System
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")> _
   <SoapEnum("Large")> _
End enum

Public Class Run
   Public Shared Sub Main()
      Dim test As Run = new Run()
      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)
   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 _
      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)

   End Sub 
End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
© 2015 Microsoft