SoapElementAttribute Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje, že hodnota veřejného členu je serializována XmlSerializer jako kódovaný element SOAP XML.
public ref class SoapElementAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class SoapElementAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type SoapElementAttribute = class
inherit Attribute
Public Class SoapElementAttribute
Inherits Attribute
- Dědičnost
- Atributy
Příklady
Následující příklad serializuje instanci třídy s názvem Transportation , která obsahuje pole s názvem Vehicle. U pole se použije A SoapElementAttribute . Pokud je pole serializováno, název elementu XML je "Wheel" místo "Vehicle". Metoda SerializeOverride vytvoří SoapElementAttribute a nastaví SoapElement vlastnost SoapAttributes na SoapElementAttributehodnotu . Přidá SoapAttributes se do objektu SoapAttributeOverrides , který slouží k vytvoření objektu XmlTypeMapping. Je XmlSerializer vytvořen s , XmlTypeMappinga instance Transportation třídy je znovu serializována.
SoapElementAttribute Vzhledem k tomu, že se používá k přepsání serializace, vygenerovaný název xml elementu je nyní "Truck" místo "Wheel".
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();
}
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Xml
Imports System.Text
Public Class Transportation
' The SoapElementAttribute specifies that the
' generated XML element name will be "Wheels"
' instead of "Vehicle".
<SoapElement("Wheels")> Public Vehicle As String
<SoapElement(DataType:= "dateTime")> _
public CreationDate As DateTime
<SoapElement(IsNullable:= true)> _
public thing As Thing
End Class
Public Class Thing
<SoapElement(IsNullable:=true)> public ThingName As string
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
t.SerializeObject("SoapElementOriginalVb.xml")
t.SerializeOverride("SoapElementOverrideVb.xml")
Console.WriteLine("Finished writing two XML files.")
End Sub
' Return an XmlSerializer used for overriding.
Public Function CreateSoapOverrider() As XmlSerializer
' Create the SoapAttributes and SoapAttributeOverrides objects.
Dim soapAttrs As SoapAttributes = New SoapAttributes()
Dim soapOverrides As SoapAttributeOverrides = _
New SoapAttributeOverrides()
' Create a SoapElementAttribute to override
' the Vehicles property.
Dim soapElement1 As SoapElementAttribute = _
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(GetType(Transportation), "Vehicle", soapAttrs)
' Create the XmlSerializer, and return it.
Dim myTypeMapping As XmlTypeMapping = (New _
SoapReflectionImporter (soapOverrides)).ImportTypeMapping _
(GetType(Transportation))
return New XmlSerializer(myTypeMapping)
End Function
Public Sub SerializeOverride(filename As String)
' Create an XmlSerializer instance.
Dim ser As XmlSerializer = CreateSoapOverrider()
' Create the object and serialize it.
Dim myTransportation As Transportation = _
New Transportation()
myTransportation.Vehicle = "MyCar"
myTransportation.CreationDate = DateTime.Now
myTransportation.thing= new Thing()
Dim writer As XmlTextWriter = _
New XmlTextWriter(filename, Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartElement("wrapper")
ser.Serialize(writer, myTransportation)
writer.WriteEndElement()
writer.Close()
End Sub
Public Sub SerializeObject(filename As String)
' Create an XmlSerializer instance.
Dim ser As XmlSerializer = _
New XmlSerializer(GetType(Transportation))
Dim myTransportation As Transportation = _
New Transportation()
myTransportation.Vehicle = "MyCar"
myTransportation.CreationDate=DateTime.Now
myTransportation.thing= new Thing()
Dim writer As XmlTextWriter = _
new XmlTextWriter(filename, Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartElement("wrapper")
ser.Serialize(writer, myTransportation)
writer.WriteEndElement()
writer.Close()
End Sub
End Class
Poznámky
Třída SoapElementAttribute patří do řady atributů, které řídí, jak XmlSerializer serializuje nebo deserializuje objekt jako kódovaný KÓD SOAP XML. Výsledný kód XML odpovídá části 5 dokumentu konsorcia World Wide Web Consortium, PROTOKOLU SOAP (Simple Object Access Protocol) 1.1. Úplný seznam podobných atributů naleznete v části Atributy, které řídí kódované soap serializace.
Chcete-li serializovat objekt jako kódovanou zprávu SOAP, je nutné vytvořit XmlSerializer pomocí XmlTypeMapping vytvořené s ImportTypeMapping metodou SoapReflectionImporter třídy.
SoapElementAttribute Použití na veřejné pole, aby bylo směrováno XmlSerializer na serializaci pole jako kódovaný element SOAP XML.
Další informace o používání atributů naleznete v tématu Atributy.
Konstruktory
| Name | Description |
|---|---|
| SoapElementAttribute() |
Inicializuje novou instanci SoapElementAttribute třídy. |
| SoapElementAttribute(String) |
Inicializuje novou instanci SoapElementAttribute třídy a určuje název elementu XML. |
Vlastnosti
| Name | Description |
|---|---|
| DataType |
Získá nebo nastaví datový typ jazyka XSD (XML Schema Definition Language) vygenerovaného elementu XML. |
| ElementName |
Získá nebo nastaví název vygenerovaného XML elementu. |
| IsNullable |
Získá nebo nastaví hodnotu, která určuje, zda XmlSerializer musí serializovat člen, který má |
| TypeId |
Při implementaci v odvozené třídě získá jedinečný identifikátor pro tento Attribute. (Zděděno od Attribute) |
Metody
| Name | Description |
|---|---|
| Equals(Object) |
Vrátí hodnotu, která určuje, zda je tato instance rovna zadanému objektu. (Zděděno od Attribute) |
| GetHashCode() |
Vrátí kód hash pro tuto instanci. (Zděděno od Attribute) |
| GetType() |
Získá Type aktuální instance. (Zděděno od Object) |
| IsDefaultAttribute() |
Při přepsání v odvozené třídě určuje, zda hodnota této instance je výchozí hodnotou pro odvozenou třídu. (Zděděno od Attribute) |
| Match(Object) |
Při přepsání v odvozené třídě vrátí hodnotu, která určuje, zda se tato instance rovná zadanému objektu. (Zděděno od Attribute) |
| MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Object. (Zděděno od Object) |
| ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
Explicitní implementace rozhraní
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Mapuje sadu názvů na odpovídající sadu identifikátorů pro rozesílání. (Zděděno od Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Načte informace o typu objektu, který lze použít k získání informací o typu pro rozhraní. (Zděděno od Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Získá počet rozhraní typu informací, které objekt poskytuje (0 nebo 1). (Zděděno od Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Poskytuje přístup k vlastnostem a metodám vystaveným objektem. (Zděděno od Attribute) |