SoapTypeAttribute 构造函数  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 SoapTypeAttribute 类的新实例。
重载
| SoapTypeAttribute() | 初始化 SoapTypeAttribute 类的新实例。 | 
| SoapTypeAttribute(String) | 初始化 SoapTypeAttribute 类的新实例,并指定 XML 类型的名称。 | 
| SoapTypeAttribute(String, String) | 初始化 SoapTypeAttribute 类的新实例,并指定类型的名称和 XML 命名空间。 | 
SoapTypeAttribute()
初始化 SoapTypeAttribute 类的新实例。
public:
 SoapTypeAttribute();public SoapTypeAttribute ();Public Sub New ()示例
The following example serializes a class named Group. 该属性 SoapTypeAttribute 应用于设置为“SoapGroupType”的 TypeName 类。 SoapTypeAttribute这也被重写,将更改为TypeName“团队”。 这两个版本都序列化,导致两个文件:SoapType.xml和SoapType2.xml。
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
[SoapType("EmployeeType")]
public ref class Employee
{
public:
   String^ Name;
};
// The SoapType is overridden when the
// SerializeOverride  method is called.
[SoapType("SoapGroupType","http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;
   array<Employee^>^Employees;
};
public ref 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 = (gcnew SoapReflectionImporter)->ImportTypeMapping( Group::typeid );
      XmlSerializer^ mySerializer = gcnew XmlSerializer( mapp );
      // Writing the file requires a TextWriter.
      TextWriter^ writer = gcnew StreamWriter( filename );
      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;
      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;
      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = 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();
   }
   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 = gcnew StreamWriter( filename );
      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;
      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;
      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = 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();
   }
   void DeserializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer^ mySerializer = CreateOverrideSerializer();
      // Reading the file requires a TextReader.
      TextReader^ reader = gcnew StreamReader( filename );
      // Create an XML text reader.
      XmlTextReader^ xmlReader = gcnew XmlTextReader( reader );
      xmlReader->ReadStartElement();
      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( xmlReader ));
      xmlReader->ReadEndElement();
      Console::WriteLine( "The GroupName is {0}", myGroup->GroupName );
      Console::WriteLine( "Look at the SoapType.xml and SoapType2.xml "
      "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 = gcnew SoapAttributeOverrides;
      SoapAttributes^ soapAtts = gcnew SoapAttributes;
      // Override the SoapTypeAttribute.
      SoapTypeAttribute^ soapType = gcnew SoapTypeAttribute;
      soapType->TypeName = "Team";
      soapType->IncludeInSchema = false;
      soapType->Namespace = "http://www.microsoft.com";
      soapAtts->SoapType = soapType;
      mySoapAttributeOverrides->Add( Group::typeid, soapAtts );
      // Create an XmlTypeMapping that is used to create an instance 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping^ myMapping = (gcnew SoapReflectionImporter( mySoapAttributeOverrides ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( myMapping );
      return ser;
   }
};
int main()
{
   Run^ test = gcnew Run;
   test->SerializeOriginal( "SoapType.xml" );
   test->SerializeOverride( "SoapType2.xml" );
   test->DeserializeObject( "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();
   }
}
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
注解
重写类型的序列化时创建一个 SoapTypeAttribute 。 将对象分配给 SoapType a SoapAttributes 属性,并将对象添加到 SoapAttributes .SoapAttributeOverrides SoapAttributeOverrides有关重写 SOAP 序列化的更多详细信息,请参阅类概述。
适用于
SoapTypeAttribute(String)
初始化 SoapTypeAttribute 类的新实例,并指定 XML 类型的名称。
public:
 SoapTypeAttribute(System::String ^ typeName);public SoapTypeAttribute (string? typeName);public SoapTypeAttribute (string typeName);new System.Xml.Serialization.SoapTypeAttribute : string -> System.Xml.Serialization.SoapTypeAttributePublic Sub New (typeName As String)参数
- typeName
- String
XmlSerializer 序列化类实例时生成(和在反序列化类实例时识别)的 XML 类型的名称。
示例
The following example serializes a class named Group. 该属性 SoapTypeAttribute 应用于设置为“SoapGroupType”的 TypeName 类。 SoapTypeAttribute这也被重写,将更改为TypeName“团队”。 这两个版本都序列化,导致两个文件:SoapType.xml和SoapType2.xml。
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
[SoapType("EmployeeType")]
public ref class Employee
{
public:
   String^ Name;
};
// The SoapType is overridden when the
// SerializeOverride  method is called.
[SoapType("SoapGroupType","http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;
   array<Employee^>^Employees;
};
public ref 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 = (gcnew SoapReflectionImporter)->ImportTypeMapping( Group::typeid );
      XmlSerializer^ mySerializer = gcnew XmlSerializer( mapp );
      // Writing the file requires a TextWriter.
      TextWriter^ writer = gcnew StreamWriter( filename );
      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;
      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;
      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = 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();
   }
   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 = gcnew StreamWriter( filename );
      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;
      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;
      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = 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();
   }
   void DeserializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer^ mySerializer = CreateOverrideSerializer();
      // Reading the file requires a TextReader.
      TextReader^ reader = gcnew StreamReader( filename );
      // Create an XML text reader.
      XmlTextReader^ xmlReader = gcnew XmlTextReader( reader );
      xmlReader->ReadStartElement();
      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( xmlReader ));
      xmlReader->ReadEndElement();
      Console::WriteLine( "The GroupName is {0}", myGroup->GroupName );
      Console::WriteLine( "Look at the SoapType.xml and SoapType2.xml "
      "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 = gcnew SoapAttributeOverrides;
      SoapAttributes^ soapAtts = gcnew SoapAttributes;
      // Override the SoapTypeAttribute.
      SoapTypeAttribute^ soapType = gcnew SoapTypeAttribute;
      soapType->TypeName = "Team";
      soapType->IncludeInSchema = false;
      soapType->Namespace = "http://www.microsoft.com";
      soapAtts->SoapType = soapType;
      mySoapAttributeOverrides->Add( Group::typeid, soapAtts );
      // Create an XmlTypeMapping that is used to create an instance 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping^ myMapping = (gcnew SoapReflectionImporter( mySoapAttributeOverrides ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( myMapping );
      return ser;
   }
};
int main()
{
   Run^ test = gcnew Run;
   test->SerializeOriginal( "SoapType.xml" );
   test->SerializeOverride( "SoapType2.xml" );
   test->DeserializeObject( "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();
   }
}
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
注解
重写类型的序列化时创建一个 SoapTypeAttribute 。 将对象分配给 SoapType a SoapAttributes 属性,并将对象添加到 SoapAttributes .SoapAttributeOverrides SoapAttributeOverrides有关重写 SOAP 序列化的更多详细信息,请参阅类概述。
适用于
SoapTypeAttribute(String, String)
初始化 SoapTypeAttribute 类的新实例,并指定类型的名称和 XML 命名空间。
public:
 SoapTypeAttribute(System::String ^ typeName, System::String ^ ns);public SoapTypeAttribute (string? typeName, string? ns);public SoapTypeAttribute (string typeName, string ns);new System.Xml.Serialization.SoapTypeAttribute : string * string -> System.Xml.Serialization.SoapTypeAttributePublic Sub New (typeName As String, ns As String)参数
- typeName
- String
XmlSerializer 序列化类实例时生成(和在反序列化类实例时识别)的 XML 类型的名称。
- ns
- String
类型的 XML 命名空间。
示例
The following example serializes a class named Group. 该属性 SoapTypeAttribute 应用于设置为“SoapGroupType”的 TypeName 类。 SoapTypeAttribute这也被重写,将更改为TypeName“团队”。 这两个版本都序列化,导致两个文件:SoapType.xml和SoapType2.xml。
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
[SoapType("EmployeeType")]
public ref class Employee
{
public:
   String^ Name;
};
// The SoapType is overridden when the
// SerializeOverride  method is called.
[SoapType("SoapGroupType","http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;
   array<Employee^>^Employees;
};
public ref 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 = (gcnew SoapReflectionImporter)->ImportTypeMapping( Group::typeid );
      XmlSerializer^ mySerializer = gcnew XmlSerializer( mapp );
      // Writing the file requires a TextWriter.
      TextWriter^ writer = gcnew StreamWriter( filename );
      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;
      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;
      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = 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();
   }
   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 = gcnew StreamWriter( filename );
      // Create an XML text writer.
      XmlTextWriter^ xmlWriter = gcnew XmlTextWriter( writer );
      xmlWriter->Formatting = Formatting::Indented;
      xmlWriter->Indentation = 2;
      // Create an instance of the class that will be serialized.
      Group^ myGroup = gcnew Group;
      // Set the Object* properties.
      myGroup->GroupName = ".NET";
      Employee^ e1 = gcnew Employee;
      e1->Name = "Pat";
      myGroup->Employees = gcnew array<Employee^>(1);
      myGroup->Employees[ 0 ] = 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();
   }
   void DeserializeObject( String^ filename )
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer^ mySerializer = CreateOverrideSerializer();
      // Reading the file requires a TextReader.
      TextReader^ reader = gcnew StreamReader( filename );
      // Create an XML text reader.
      XmlTextReader^ xmlReader = gcnew XmlTextReader( reader );
      xmlReader->ReadStartElement();
      // Deserialize and cast the object.
      Group^ myGroup;
      myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( xmlReader ));
      xmlReader->ReadEndElement();
      Console::WriteLine( "The GroupName is {0}", myGroup->GroupName );
      Console::WriteLine( "Look at the SoapType.xml and SoapType2.xml "
      "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 = gcnew SoapAttributeOverrides;
      SoapAttributes^ soapAtts = gcnew SoapAttributes;
      // Override the SoapTypeAttribute.
      SoapTypeAttribute^ soapType = gcnew SoapTypeAttribute;
      soapType->TypeName = "Team";
      soapType->IncludeInSchema = false;
      soapType->Namespace = "http://www.microsoft.com";
      soapAtts->SoapType = soapType;
      mySoapAttributeOverrides->Add( Group::typeid, soapAtts );
      // Create an XmlTypeMapping that is used to create an instance 
      // of the XmlSerializer. Then return the XmlSerializer Object*.
      XmlTypeMapping^ myMapping = (gcnew SoapReflectionImporter( mySoapAttributeOverrides ))->ImportTypeMapping( Group::typeid );
      XmlSerializer^ ser = gcnew XmlSerializer( myMapping );
      return ser;
   }
};
int main()
{
   Run^ test = gcnew Run;
   test->SerializeOriginal( "SoapType.xml" );
   test->SerializeOverride( "SoapType2.xml" );
   test->DeserializeObject( "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();
   }
}
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
注解
重写类型的序列化时创建一个 SoapTypeAttribute 。 将对象分配给 SoapType a SoapAttributes 属性,并将对象添加到 SoapAttributes .SoapAttributeOverrides SoapAttributeOverrides有关重写 SOAP 序列化的更多详细信息,请参阅类概述。
如果为多个类型设置 Namespace 值 (即,如果将属性应用于具有) 不同 Namespace 值的多个类,则 XML 架构定义工具 (Xsd.exe) 为每个类型生成单独的架构文件 (.xsd) 。 这是因为,为每个类型设置不同的命名空间会呈现不同于其他类型的不同类型,这使得每个类型都需要作为独立实体写出。