Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SoapTypeAttribute Class

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

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Interface)]
public class SoapTypeAttribute : Attribute

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 [<topic://cpconAttributesThatControlSOAPEncodedSerialization>].

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 [<topic://cpconXMLSchemaDefinitionToolXsdexe>]. 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.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// The SoapType is overridden when the 
// SerializeOverride  method is called.
[SoapType("SoapGroupType", "http://www.cohowinery.com")]
public class Group
{
   public string GroupName;
   public Employee[] Employees;
}

[SoapType("EmployeeType")]
public class Employee
{
   public string Name;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeOriginal("SoapType.xml");
      test.SerializeOverride("SoapType2.xml");
      test.DeserializeObject("SoapType2.xml");
   }

   public void SerializeOriginal(string filename)
   {
      // Create an instance of the XmlSerializer class that 
      // can be used for serializing as a SOAP message.
      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 XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";
      Employee e1 = 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();
   }


   public void SerializeOverride(string filename)
   {
      // Create an instance of the XmlSerializer class that 
      // uses a SoapAttributeOverrides object.

      XmlSerializer mySerializer =  CreateOverrideSerializer();

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an XML text writer.
      XmlTextWriter xmlWriter = new XmlTextWriter(writer);
      xmlWriter.Formatting = Formatting.Indented;
      xmlWriter.Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";
      Employee e1 = 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();
   }

   private XmlSerializer CreateOverrideSerializer()
   {
      // Create and return an XmlSerializer instance used to 
      // override and create SOAP messages.
      SoapAttributeOverrides mySoapAttributeOverrides = 
          new SoapAttributeOverrides();
      SoapAttributes soapAtts = new SoapAttributes();

      // Override the SoapTypeAttribute.
      SoapTypeAttribute soapType = new SoapTypeAttribute();
      soapType.TypeName = "Team";
      soapType.IncludeInSchema = false;
      soapType.Namespace = "http://www.microsoft.com";
      soapAtts.SoapType = soapType;

      mySoapAttributeOverrides.Add(typeof(Group),soapAtts);

      // Create an XmlTypeMapping that is used to create an instance  
      // of the XmlSerializer. Then return the XmlSerializer object.
      XmlTypeMapping myMapping = (new SoapReflectionImporter(
      mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));

      XmlSerializer ser = new XmlSerializer(myMapping);
      return ser;
   }

   public void DeserializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrideSerializer();

      // Reading the file requires a TextReader.
      TextReader reader = new StreamReader(filename);

      // Create an XML text reader.
      XmlTextReader xmlReader = new XmlTextReader(reader);
      xmlReader.ReadStartElement();

      // Deserialize and cast the object.
      Group myGroup = (Group) mySerializer.Deserialize(xmlReader);
      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();
   }
}
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

[SoapType(S"EmployeeType")]
__gc public class Employee
{
public:
   String* Name;
};

// The SoapType is overridden when the
// SerializeOverride  method is called.
[SoapType(S"SoapGroupType", S"http://www.cohowinery.com")]
__gc public class Group
{
public:
   String* GroupName;
   Employee * Employees[];
};

__gc public class Run
{
public:
   void SerializeOriginal(String* filename)
   {
      // Create an instance of the XmlSerializer class that
      // can be used for serializing as a SOAP message.
      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 XML text writer.
      XmlTextWriter* xmlWriter = new XmlTextWriter(writer);
      xmlWriter -> Formatting = Formatting::Indented;
      xmlWriter -> Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group* myGroup = new Group();

      // Set the Object* properties.
      myGroup -> GroupName = S".NET";
      Employee* e1 = new Employee();
      e1 -> Name = S"Pat";
      myGroup -> Employees = new Employee*[1];
      myGroup -> Employees[0] = e1;

      // Write the root element.
      xmlWriter -> WriteStartElement(S"root");

      // Serialize the class.
      mySerializer -> Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter -> WriteEndElement();

      // Close the XmlWriter.
      xmlWriter -> Close();

      // Close the TextWriter.
      writer -> Close();
   }

   void SerializeOverride(String* filename) 
   {
      // Create an instance of the XmlSerializer class that
      // uses a SoapAttributeOverrides Object*.

      XmlSerializer * mySerializer =  CreateOverrideSerializer();

      // Writing the file requires a TextWriter.
      TextWriter* writer = new StreamWriter(filename);

      // Create an XML text writer.
      XmlTextWriter* xmlWriter = new XmlTextWriter(writer);
      xmlWriter -> Formatting = Formatting::Indented;
      xmlWriter -> Indentation = 2;

      // Create an instance of the class that will be serialized.
      Group* myGroup = new Group();

      // Set the Object* properties.
      myGroup -> GroupName = S".NET";
      Employee* e1 = new Employee();
      e1 -> Name = S"Pat";
      myGroup -> Employees = new Employee*[1];
      myGroup -> Employees[0] = e1;

      // Write the root element.
      xmlWriter -> WriteStartElement(S"root");

      // Serialize the class.
      mySerializer -> Serialize(xmlWriter, myGroup);

      // Close the root tag.
      xmlWriter -> WriteEndElement();

      // Close the XmlWriter.
      xmlWriter -> Close();

      // Close the TextWriter.
      writer -> Close();
   }

   void DeserializeObject(String* filename) 
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer * mySerializer = CreateOverrideSerializer();

      // Reading the file requires a TextReader.
      TextReader* reader = new StreamReader(filename);

      // Create an XML text reader.
      XmlTextReader* xmlReader = new XmlTextReader(reader);
      xmlReader -> ReadStartElement();

      // Deserialize and cast the object.
      Group * myGroup; 
      myGroup = dynamic_cast<Group*>(mySerializer -> Deserialize(xmlReader));
      xmlReader -> ReadEndElement();
      Console::WriteLine(S"The GroupName is {0}", myGroup -> GroupName);
      Console::WriteLine(
        S"Look at the SoapType.xml and SoapType2.xml " 
        S"files for the generated XML.");

      // Close the readers.
      xmlReader -> Close();
      reader -> Close();
   }

private:
   XmlSerializer * CreateOverrideSerializer() 
   {
      // Create and return an XmlSerializer instance used to
      //  and create SOAP messages.
      SoapAttributeOverrides* mySoapAttributeOverrides = new SoapAttributeOverrides();
      SoapAttributes* soapAtts = new SoapAttributes();

      // Override the SoapTypeAttribute.
      SoapTypeAttribute* soapType = new SoapTypeAttribute();
      soapType -> TypeName = S"Team";
      soapType -> IncludeInSchema = false;
      soapType -> Namespace = S"http://www.microsoft.com";
      soapAtts -> SoapType = soapType;

      mySoapAttributeOverrides -> Add(__typeof(Group), soapAtts);

      // Create an XmlTypeMapping that is used to create an instance 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping * myMapping = (new SoapReflectionImporter(mySoapAttributeOverrides)) -> ImportTypeMapping(__typeof(Group));

      XmlSerializer* ser = new XmlSerializer(myMapping);
      return ser;
   }
};

int main() 
{
   Run* test = new Run();
   test -> SerializeOriginal(S"SoapType.xml");
   test -> SerializeOverride(S"SoapType2.xml");
   test -> DeserializeObject(S"SoapType2.xml");
}
System.Object
  System.Attribute
    System.Xml.Serialization.SoapTypeAttribute
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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.