SoapTypeAttribute Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Contrôle le schéma généré par l'XmlSerializer lorsqu’une instance de classe est sérialisée en tant que code XML encodé SOAP.
public ref class SoapTypeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
public class SoapTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
type SoapTypeAttribute = class
inherit Attribute
Public Class SoapTypeAttribute
Inherits Attribute
- Héritage
- Attributs
Exemples
L’exemple suivant sérialise une classe nommée Group. L’objet SoapTypeAttribute est appliqué à la classe, avec la TypeName valeur « SoapGroupType ». Il SoapTypeAttribute est également substitué, en remplaçant l’option TypeName « Team ». Les deux versions sont sérialisées, ce qui entraîne deux fichiers : SoapType.xml et 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
Remarques
La SoapTypeAttribute classe appartient à une famille d’attributs qui contrôle la façon dont la XmlSerializer sérialise ou désérialise un objet en tant que xml SOAP codé. Le code XML résultant est conforme à la section 5 du document World Wide Web Consortium, SOAP (Simple Object Access Protocol) 1.1. Pour obtenir la liste complète des attributs similaires, consultez Attributs qui contrôlent la sérialisation SOAP encodée.
Pour sérialiser un objet en tant que message SOAP encodé, construisez l’utilisation XmlSerializer d’un XmlTypeMapping objet créé avec la ImportTypeMapping méthode de la SoapReflectionImporter classe.
Il SoapTypeAttribute ne peut être appliqué qu’aux déclarations de classe.
La IncludeInSchema propriété détermine si le type d’élément XML résultant est inclus dans le document de schéma XML (.xsd) pour le flux XML généré. Pour afficher le schéma, compilez la classe dans un fichier DLL. Transmettez le fichier résultant en tant qu’argument à l’outil de définition de schéma XML (Xsd.exe). L’outil génère le schéma XML pour le flux XML généré lorsque la classe est sérialisée par une instance de la XmlSerializer classe.
La définition d’un espace de noms différent entraîne l'Xsd.exe écriture d’un fichier de schéma (.xsd) différent pour le flux XML généré lorsque la classe est sérialisée.
Constructeurs
| Nom | Description |
|---|---|
| SoapTypeAttribute() |
Initialise une nouvelle instance de la classe SoapTypeAttribute. |
| SoapTypeAttribute(String, String) |
Initialise une nouvelle instance de la SoapTypeAttribute classe et spécifie le nom et l’espace de noms XML du type. |
| SoapTypeAttribute(String) |
Initialise une nouvelle instance de la SoapTypeAttribute classe et spécifie le nom du type XML. |
Propriétés
| Nom | Description |
|---|---|
| IncludeInSchema |
Obtient ou définit une valeur qui indique s’il faut inclure le type dans les documents de schéma XML codés en SOAP. |
| Namespace |
Obtient ou définit l’espace de noms du type XML. |
| TypeId |
En cas d’implémentation dans une classe dérivée, obtient un identificateur unique pour cette Attribute. (Hérité de Attribute) |
| TypeName |
Obtient ou définit le nom du type XML. |
Méthodes
| Nom | Description |
|---|---|
| Equals(Object) |
Retourne une valeur qui indique si cette instance est égale à un objet spécifié. (Hérité de Attribute) |
| GetHashCode() |
Retourne le code de hachage pour cette instance. (Hérité de Attribute) |
| GetType() |
Obtient la Type de l’instance actuelle. (Hérité de Object) |
| IsDefaultAttribute() |
En cas de substitution dans une classe dérivée, indique si la valeur de cette instance est la valeur par défaut de la classe dérivée. (Hérité de Attribute) |
| Match(Object) |
En cas de substitution dans une classe dérivée, retourne une valeur qui indique si cette instance est égale à un objet spécifié. (Hérité de Attribute) |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| ToString() |
Retourne une chaîne qui représente l’objet actuel. (Hérité de Object) |
Implémentations d’interfaces explicites
| Nom | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Mappe un jeu de noms avec un jeu correspondant d'identificateurs de dispatch. (Hérité de Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Récupère les informations de type d’un objet, qui peuvent être utilisées pour obtenir les informations de type d’une interface. (Hérité de Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Récupère le nombre d'interfaces d'informations de type fourni par un objet (0 ou 1). (Hérité de Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Fournit l’accès aux propriétés et méthodes exposées par un objet. (Hérité de Attribute) |