XmlElementAttribute.DataType Eigenschaft
Definition
Ruft den XSD (XML Schema Definition)-Datentyp des vom XmlSerializer generierten XML-Elements ab oder legt diesen fest.Gets or sets the XML Schema definition (XSD) data type of the XML element generated by the 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
Eigenschaftswert
Ein XML-Schema Datentyp.An XML Schema data type.
Ausnahmen
Der angegebene XML-Schemadatentyp kann dem .NET-Datentyp nicht zugeordnet werden.The XML Schema data type you have specified cannot be mapped to the.NET data type.
Beispiele
Im folgenden Beispiel wird eine Klasse mit dem Group
Namen serialisiert, die ExtraInfo
ein Feld mit dem ArrayListNamen enthält, das einen zurückgibt.The following example serializes a class named Group
that contains a field named ExtraInfo
, which returns an ArrayList. Im Beispiel werden zwei Instanzen von XmlElementAttribute auf das-Feld angewendet, und es werden unterschiedliche DataType Werte für jede Instanz angegeben.The example applies two instances of the XmlElementAttribute to the field and specifies different DataType values for each instance. Jede-Instanz ermöglicht XmlSerializer das Serialisieren der in das Array eingefügten angegebenen Typen.Each instance enables the XmlSerializer to serialize the specified types inserted into the array.
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Group
{
public:
/* 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=String::typeid,
Namespace="http://www.cpandl.com"),
XmlElement(DataType="snippet1>",
Namespace="http://www.cohowinery.com",
Type=Int32::typeid)]
ArrayList^ ExtraInfo;
};
void SerializeObject( String^ filename )
{
// A TextWriter is needed to write the file.
TextWriter^ writer = gcnew StreamWriter( filename );
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );
// Create the object to serialize.
Group^ myGroup = gcnew Group;
/* Add a string and an integer to the ArrayList returned
by the ExtraInfo field. */
myGroup->ExtraInfo = gcnew ArrayList;
myGroup->ExtraInfo->Add( "hello" );
myGroup->ExtraInfo->Add( 100 );
// Serialize the object and close the TextWriter.
s->Serialize( writer, myGroup );
writer->Close();
}
int main()
{
SerializeObject( "ElementTypes.xml" );
}
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
Hinweise
In der folgenden Tabelle sind die einfachen XML-Schema Datentypen mit their.NET-Entsprechungen aufgelistet.The following table lists the XML Schema simple data types with their.NET equivalents.
Verwenden Sie für das base64Binary
XML hexBinary
-Schema und die Datentypen ein Byte Array von-Strukturen, XmlElementAttribute und wenden DataType Sie ein-Array mit dem auf "base64Binary" oder "hexBinary" festgelegten nach Bedarf an.For the XML Schema base64Binary
and hexBinary
data types, use an array of Byte structures, and apply a XmlElementAttribute with the DataType set to "base64Binary" or "hexBinary", as appropriate. Verwenden Sie für das time
XML date
-Schema und die Daten DateTime Typen den-Typ XmlElementAttribute , und DataType wenden Sie das mit dem Satz auf "Date" oder "Time" an.For the XML Schema time
and date
data types, use the DateTime type and apply the XmlElementAttribute with the DataType set to "date" or "time".
Wenden Sie für jeden XML-Schematyp, der einer Zeichenfolge XmlElementAttribute zugeordnet ist DataType , das-Objekt mit der-Eigenschaft auf den XML-Schematyp fest.For every XML Schema type that is mapped to a string, apply the XmlElementAttribute with its DataType property set to the XML Schema type. Es ist möglich, dass dadurch das Serialisierungsformat geändert werden kann, nicht nur das Schema für den Member.It is possible that this can change the serialization format, not only the schema for the member.
Hinweis
Bei der-Eigenschaft wird die Groß-/Kleinschreibung beachtet, sodass Sie Sie genau auf einen der XML-Schema Datentypen festlegen müssen.The property is case-sensitive, so you must set it exactly to one of the XML Schema data types.
Hinweis
Das übergeben von Binärdaten als XML-Element ist effizienter als das übergeben als XML-Schema Attribut.Passing binary data as an XML element is more efficient than passing it as an XML Schema attribute.
Weitere Informationen zu XML-Datentypen finden Sie im World Wide Web Consortium Dokument mit dem Namen XML Schema Part 2: Datatypes.For more information about XML data types, see the World Wide Web Consortium document named XML Schema Part 2: Datatypes.
XSD-DatentypXSD data type | .NET-Datentyp.NET data type |
---|---|
anyURIanyURI | String |
base64Binarybase64Binary | Array von Byte ObjektenArray of Byte objects |
booleanboolean | Boolean |
bytebyte | SByte |
datedate | DateTime |
dateTimedateTime | DateTime |
decimaldecimal | Decimal |
doubledouble | Double |
ENTITYENTITY | String |
KLEINSTUNTERNEHMENENTITIES | String |
floatfloat | Single |
gDaygDay | String |
gMonthgMonth | String |
gMonthDaygMonthDay | String |
gYeargYear | String |
gYearMonthgYearMonth | String |
hexBinaryhexBinary | Array von Byte ObjektenArray of Byte objects |
IdID | String |
IDREFIDREF | String |
IDREFSIDREFS | String |
intint | Int32 |
Ganze Zahlinteger | String |
languagelanguage | String |
longlong | Int64 |
nameName | String |
NCNameNCName | String |
negativeIntegernegativeInteger | String |
NMTOKENNMTOKEN | String |
NMTOKENSNMTOKENS | String |
normalizedStringnormalizedString | String |
nonNegativeIntegernonNegativeInteger | String |
nonPositiveIntegernonPositiveInteger | String |
NOTATIONNOTATION | String |
positiveIntegerpositiveInteger | String |
QNameQName | XmlQualifiedName |
durationduration | String |
stringstring | String |
shortshort | Int16 |
Uhrzeittime | DateTime |
tokentoken | String |
unsignedByteunsignedByte | Byte |
unsignedIntunsignedInt | UInt32 |
unsignedLongunsignedLong | UInt64 |
unsignedShortunsignedShort | UInt16 |