SoapElementAttribute.DataType Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene o imposta il tipo di dati XSD (XML Schema Definition Language) dell'elemento XML generato.
public:
property System::String ^ DataType { System::String ^ get(); void set(System::String ^ value); };
public string DataType { get; set; }
member this.DataType : string with get, set
Public Property DataType As String
Valore della proprietà
Uno dei tipi di dati XML Schema.
Esempio
Nell'esempio seguente viene serializzata un'istanza di una classe denominata Transportation che contiene un campo denominato Vehicle. Un SoapElementAttribute oggetto viene applicato al campo . Quando il campo viene serializzato, il nome dell'elemento XML è "Wheels" anziché "Vehicle". Il SerializeOverride metodo crea un oggetto SoapElementAttribute e imposta la SoapElement proprietà di un SoapAttributes oggetto SoapElementAttributesu . Viene SoapAttributes aggiunto a un SoapAttributeOverrides oggetto utilizzato per creare un oggetto XmlTypeMapping. Un XmlSerializer oggetto viene costruito con e un'istanza XmlTypeMappingdella Transportation classe viene nuovamente serializzata. Poiché viene usato per eseguire l'override SoapElementAttribute della serializzazione, il nome dell'elemento XML generato è ora "Truck" anziché "Wheels".
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
Commenti
Nella tabella seguente sono elencati i tipi di dati semplici di XML Schema con i relativi equivalenti .NET.
Per i tipi di dati e schema XMLbase64Binary, utilizzare una matrice di Byte strutture e applicare un SoapElementAttribute oggetto con il DataType valore impostato su "base64Binary" o "hexBinary", a seconda delle hexBinary esigenze. Per xml Schema time e date tipi di dati, utilizzare il DateTime tipo e applicare SoapElementAttribute con il DataType valore impostato su "date" o "time".
Per ogni tipo di dati XML Schema mappato a una stringa, applicare con la SoapElementAttribute relativa DataType proprietà impostata sul tipo di XML Schema. Si noti che questo non modifica il formato di serializzazione, ma solo lo schema per il membro.
Annotazioni
La proprietà fa distinzione tra maiuscole e minuscole, pertanto è necessario impostarla esattamente su uno dei tipi di dati XML Schema.
Annotazioni
Il passaggio di dati binari come elemento XML è più efficiente rispetto al passaggio come attributo XML.
Per altre informazioni sui tipi di dati XML, vedere il documento World Wide Web Consortium, XML Schema Part 2: Datatypes.
| Tipo di dati XSD | Tipo di dati .NET |
|---|---|
| anyURI | String |
| base64Binary | Matrice di Byte oggetti |
| boolean | Boolean |
| byte | SByte |
| date | DateTime |
| data e ora | DateTime |
| decimale | Decimal |
| doppio | Double |
| ENTITÀ | String |
| ENTITÀ | String |
| galleggiare | Single |
| gDay | String |
| gMonth | String |
| gMonthDay | String |
| gYear | String |
| AnnoMese | String |
| Hexbinary | Matrice di Byte oggetti |
| Documento d'identità | String |
| IDREF | String |
| IDREFS | String |
| int | Int32 |
| numero intero | String |
| language | String |
| lungo | Int64 |
| Nome | String |
| Ncname | String |
| negativeInteger | String |
| NMTOKEN | String |
| NMTOKENS | String |
| normalizedString | String |
| nonNegativeInteger | String |
| nonPositiveInteger | String |
| NOTAZIONE | String |
| positiveInteger | String |
| QName | XmlQualifiedName |
| duration | String |
| string | String |
| short | Int16 |
| Tempo | DateTime |
| token | String |
| byte senza segno | Byte |
| intero non firmato | UInt32 |
| intero lungo non firmato | UInt64 |
| unsignedShort (numero breve senza segno) | UInt16 |