SoapTypeAttribute Class

Controls the schema generated by the XmlSerializer when a class instance is serialized as SOAP encoded XML.

System.Object
  System.Attribute
    System.Xml.Serialization.SoapTypeAttribute

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

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Enum Or AttributeTargets.Interface)> _
Public Class SoapTypeAttribute _
	Inherits Attribute

The SoapTypeAttribute type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSoapTypeAttributeInitializes a new instance of the SoapTypeAttribute class.
Public methodSupported by the XNA FrameworkSoapTypeAttribute(String)Initializes a new instance of the SoapTypeAttribute class and specifies the name of the XML type.
Public methodSupported by the XNA FrameworkSoapTypeAttribute(String, String)Initializes a new instance of the SoapTypeAttribute class and specifies the name and XML namespace of the type.
Top

  NameDescription
Public propertyIncludeInSchemaGets or sets a value that indicates whether to include the type in SOAP-encoded XML Schema documents.
Public propertySupported by the XNA FrameworkNamespaceGets or sets the namespace of the XML type.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public propertySupported by the XNA FrameworkTypeNameGets or sets the name of the XML type.
Top

  NameDescription
Public methodSupported by the XNA FrameworkEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

The SoapTypeAttribute 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 Attributes That Control Encoded SOAP Serialization.

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

The SoapTypeAttribute can only be applied to class declarations.

The IncludeInSchema property determines whether the resulting XML element type is included in the XML Schema document (.xsd) for the generated XML stream. To see the schema, compile the class into a DLL file. Pass the resulting file as an argument to the XML Schema Definition Tool (Xsd.exe). The tool generates the XML Schema for the XML stream generated when the class is serialized by an instance of the XmlSerializer class.

Setting a different namespace causes Xsd.exe to write a different schema (.xsd) file for the XML stream generated when the class is serialized.

The following example serializes a class named Group. The SoapTypeAttribute is applied to the class, with the TypeName set to "SoapGroupType". The SoapTypeAttribute is also overridden, changing the TypeName to "Team". Both versions are serialized, resulting in two files: SoapType.xml and SoapType2.xml.

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft