Lire en anglais

Partager via


SoapElementAttribute Classe

Définition

Spécifie que la valeur du membre public doit être sérialisée par XmlSerializer comme un élément XML SOAP encodé.

C#
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class SoapElementAttribute : Attribute
Héritage
SoapElementAttribute
Attributs

Exemples

L’exemple suivant sérialise une instance d’une classe nommée Transportation qui contient un champ nommé Vehicle. Un SoapElementAttribute est appliqué au champ. Lorsque le champ est sérialisé, le nom de l’élément XML est « Wheel » au lieu de « Véhicule ». La SerializeOverride méthode crée un SoapElementAttribute et définit la SoapElement propriété d’un SoapAttributes sur le SoapElementAttribute. L’élément SoapAttributes est ajouté à un SoapAttributeOverrides qui est utilisé pour créer un XmlTypeMapping. Un XmlSerializer est construit avec le XmlTypeMapping, et une instance de la Transportation classe est à nouveau sérialisée. Comme il SoapElementAttribute est utilisé pour remplacer la sérialisation, le nom de l’élément XML généré est désormais « Truck » au lieu de « Wheel ».

C#
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;
using System.Text;
public class Transportation
{
   // The SoapElementAttribute specifies that the
   // generated XML element name will be "Wheels"
   // instead of "Vehicle".
   [SoapElement("Wheels")]
   public string Vehicle;
   [SoapElement(DataType = "dateTime")]
   public DateTime CreationDate;
   [SoapElement(IsNullable = true)]
   public Thing thing;
}

public class Thing{
   [SoapElement(IsNullable=true)] public string ThingName;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("SoapElementOriginal.xml");
      t.SerializeOverride("SoapElementOverride.xml");
      Console.WriteLine("Finished writing two XML files.");
   }

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateSoapOverrider()
   {
      // Create the SoapAttributes and SoapAttributeOverrides objects.
      SoapAttributes soapAttrs = new SoapAttributes();

      SoapAttributeOverrides soapOverrides =
      new SoapAttributeOverrides();

      /* Create an SoapElementAttribute to override
      the Vehicles property. */
      SoapElementAttribute soapElement1 =
      new SoapElementAttribute("Truck");
      // Set the SoapElement to the object.
      soapAttrs.SoapElement= soapElement1;

      /* Add the SoapAttributes to the SoapAttributeOverrides,
      specifying the member to override. */
      soapOverrides.Add(typeof(Transportation), "Vehicle", soapAttrs);

      // Create the XmlSerializer, and return it.
      XmlTypeMapping myTypeMapping = (new SoapReflectionImporter
      (soapOverrides)).ImportTypeMapping(typeof(Transportation));
      return new XmlSerializer(myTypeMapping);
   }

   public void SerializeOverride(string filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer ser = CreateSoapOverrider();

      // Create the object and serialize it.
      Transportation myTransportation =
      new Transportation();

      myTransportation.Vehicle = "MyCar";
      myTransportation.CreationDate=DateTime.Now;
      myTransportation.thing = new Thing();

      XmlTextWriter writer =
      new XmlTextWriter(filename, Encoding.UTF8);
      writer.Formatting = Formatting.Indented;
      writer.WriteStartElement("wrapper");
      ser.Serialize(writer, myTransportation);
      writer.WriteEndElement();
      writer.Close();
   }
   public void SerializeObject(string filename){
      // Create an XmlSerializer instance.
      XmlSerializer ser = new XmlSerializer(typeof(Transportation));
      Transportation myTransportation =
      new Transportation();
      myTransportation.Vehicle = "MyCar";
      myTransportation.CreationDate = DateTime.Now;
      myTransportation.thing = new Thing();
      XmlTextWriter writer =
      new XmlTextWriter(filename, Encoding.UTF8);
      writer.Formatting = Formatting.Indented;
      writer.WriteStartElement("wrapper");
      ser.Serialize(writer, myTransportation);
      writer.WriteEndElement();
      writer.Close();
   }
}

Remarques

La SoapElementAttribute 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 code SOAP XML encodé. Le code XML obtenu 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é, vous devez construire l’utilisation XmlSerializer d’un XmlTypeMapping objet créé avec la ImportTypeMapping méthode de la SoapReflectionImporter classe.

Appliquez le SoapElementAttribute champ public pour diriger le XmlSerializer champ pour sérialiser le champ en tant qu’élément XML SOAP encodé.

Pour plus d’informations sur l’utilisation d’attributs, consultez Attributs.

Constructeurs

SoapElementAttribute()

Initialise une nouvelle instance de la classe SoapElementAttribute.

SoapElementAttribute(String)

Initialise une nouvelle instance de la classe SoapElementAttribute et spécifie le nom de l'élément XML.

Propriétés

DataType

Obtient ou définit le type de données XSD (XML Schema Definition) de l'élément XML généré.

ElementName

Obtient ou définit le nom de l'élément XML généré.

IsNullable

Obtient ou définit une valeur qui indique si le XmlSerializer doit sérialiser un membre dont l'attribut xsi:null a la valeur "1."

TypeId

Lors de l'implémentation dans une classe dérivée, obtient un identificateur unique pour l'objet Attribute.

(Hérité de Attribute)

Méthodes

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 de cette instance.

(Hérité de Attribute)
GetType()

Obtient le 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 pour la classe dérivée.

(Hérité de Attribute)
Match(Object)

En cas de substitution dans une classe dérivée, retourne une valeur indiquant si cette instance équivaut à un objet spécifié.

(Hérité de Attribute)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

Implémentations d’interfaces explicites

_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 pour 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 aux méthodes exposées par un objet.

(Hérité de Attribute)

S’applique à

Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1