XmlElementAttribute.DataType Eigenschap
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee wordt het XSD-gegevenstype (XML Schema Definition) opgehaald of ingesteld van het XML-element dat wordt gegenereerd door het XmlSerializer.
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
Waarde van eigenschap
Een xml-schemagegevenstype.
Uitzonderingen
Het gegevenstype XML-schema dat u hebt opgegeven, kan niet worden toegewezen aan het gegevenstype.NET.
Voorbeelden
In het volgende voorbeeld wordt een klasse geserialiseerd Group die een veld met de naam ExtraInfobevat, dat een ArrayListveld retourneert. In het voorbeeld worden twee exemplaren van het XmlElementAttribute veld toegepast en worden verschillende DataType waarden voor elk exemplaar opgegeven. Met elk exemplaar kunnen de XmlSerializer opgegeven typen die zijn ingevoegd in de matrix serialiseren.
using System;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
public class Group
{
/* Apply two XmlElementAttributes to the field. Set the DataType
to string an int to allow the ArrayList to accept
both types. The Namespace is also set to different values
for each type. */
[XmlElement(DataType = "string",
Type = typeof(string),
Namespace = "http://www.cpandl.com"),
XmlElement(DataType = "int",
Namespace = "http://www.cohowinery.com",
Type = typeof(int))]
public ArrayList ExtraInfo;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("ElementTypes.xml");
}
public void SerializeObject(string filename)
{
// A TextWriter is needed to write the file.
TextWriter writer = new StreamWriter(filename);
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Group));
// Create the object to serialize.
Group myGroup = new Group();
/* Add a string and an integer to the ArrayList returned
by the ExtraInfo field. */
myGroup.ExtraInfo = new ArrayList();
myGroup.ExtraInfo.Add("hello");
myGroup.ExtraInfo.Add(100);
// Serialize the object and close the TextWriter.
s.Serialize(writer,myGroup);
writer.Close();
}
}
Imports System.Collections
Imports System.IO
Imports System.Xml.Serialization
Public Class Group
' Apply two XmlElementAttributes to the field. Set the DataType
' to string and int to allow the ArrayList to accept
' both types. The Namespace is also set to different values
' for each type.
<XmlElement(DataType := "string", _
Type := GetType(String), _
Namespace := "http://www.cpandl.com"), _
XmlElement(DataType := "int", _
Type := GetType(Integer), _
Namespace := "http://www.cohowinery.com")> _
Public ExtraInfo As ArrayList
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("ElementTypes.xml")
End Sub
Public Sub SerializeObject(filename As String)
' A TextWriter is needed to write the file.
Dim writer As New StreamWriter(filename)
' Create the XmlSerializer using the XmlAttributeOverrides.
Dim s As New XmlSerializer(GetType(Group))
' Create the object to serialize.
Dim myGroup As New Group()
' Add a string and an integer to the ArrayList returned
' by the ExtraInfo field.
myGroup.ExtraInfo = New ArrayList()
myGroup.ExtraInfo.Add("hello")
myGroup.ExtraInfo.Add(100)
' Serialize the object and close the TextWriter.
s.Serialize(writer, myGroup)
writer.Close()
End Sub
End Class
Opmerkingen
De volgende tabel bevat de eenvoudige gegevenstypen van het XML-schema met de bijbehorende equivalenten.NET.
Voor het XML-schema base64Binary en hexBinary de gegevenstypen gebruikt u een matrix met Byte structuren en past u een XmlElementAttribute met de DataType set toe op "base64Binary" of "hexBinary", indien van toepassing. Gebruik voor het XML-schema time en date de gegevenstypen het DateTime type en pas het XmlElementAttribute toe met de DataType set op 'datum' of 'tijd'.
Voor elk XML-schematype dat is toegewezen aan een tekenreeks, past u de eigenschap met XmlElementAttribute de DataType eigenschap die is ingesteld op het XML-schematype toe. Het is mogelijk dat dit de serialisatie-indeling kan wijzigen, niet alleen het schema voor het lid.
Note
De eigenschap is hoofdlettergevoelig, dus u moet deze exact instellen op een van de gegevenstypen van het XML-schema.
Note
Het doorgeven van binaire gegevens als een XML-element is efficiƫnter dan het doorgeven als een XML-schemakenmerk.
Zie het World Wide Web Consortium-document met de naam XML-schema deel 2: Gegevenstypen voor meer informatie over XML-gegevenstypen.
| XSD-datatype | .NET-gegevenstype |
|---|---|
| anyURI | String |
| base64Binary | Matrix van Byte objecten |
| boolean | Boolean |
| byte | SByte |
| date | DateTime |
| dateTime | DateTime |
| decimal | Decimal |
| dubbel | Double |
| ENTITEIT | String |
| ENTITEITEN | String |
| zweven | Single |
| gDay | String |
| gMonth | String |
| gMonthDay | String |
| gYear | String |
| gYearMonth | String |
| hexBinary | Matrix van Byte objecten |
| Id | String |
| IDREF | String |
| IDREFS | String |
| int | Int32 |
| integer | String |
| language | String |
| long | Int64 |
| Naam | String |
| NCName | String |
| negativeInteger | String |
| NMTOKEN | String |
| NMTOKENS | String |
| normalizedString | String |
| nonNegativeInteger | String |
| nonPositiveInteger | String |
| NOTATIE | String |
| positiveInteger | String |
| QName | XmlQualifiedName |
| duration | String |
| touw | String |
| kort | Int16 |
| time | DateTime |
| token | String |
| ongetekend byte | Byte |
| unsignedInt (niet-ondertekend geheel getal) | UInt32 |
| unsignedLong | UInt64 |
| unsignedShort | UInt16 |