XmlElementAttribute.DataType Vlastnost
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í.
Získá nebo nastaví xml schema definice (XSD) datový typ XML element vygenerovaný 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
Hodnota vlastnosti
Datový typ schématu XML.
Výjimky
Zadaný datový typ schématu XML nelze mapovat na the.NET datový typ.
Příklady
Následující příklad serializuje třídu s názvem Group , která obsahuje pole s názvem ExtraInfo, který vrací ArrayList. Příklad použije dvě instance XmlElementAttribute pole a určuje různé DataType hodnoty pro každou instanci. Každá instance umožňuje XmlSerializer serializovat zadané typy vložené do pole.
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
Poznámky
Následující tabulka uvádí jednoduché datové typy schématu XML s ekvivalenty their.NET.
V případě schématu base64Binary XML a hexBinary datových Byte typů použijte pole struktur a podle potřeby použijte XmlElementAttribute pole se DataType sadou "base64Binary" nebo "hexBinary". Pro schéma time XML a date datové typy použijte DateTime typ a použijte XmlElementAttribute s DataType nastaveným datem nebo časem.
Pro každý typ schématu XML, který je mapován na řetězec, použijte XmlElementAttribute s jeho DataType vlastností nastavenou na typ schématu XML. Je možné, že to může změnit formát serializace, nejen schéma člena.
Poznámka:
Vlastnost rozlišují malá a velká písmena, takže ji musíte nastavit přesně na jeden z datových typů schématu XML.
Poznámka:
Předávání binárních dat jako elementu XML je efektivnější než jejich předání jako atribut schématu XML.
Další informace o datových typech XML naleznete v dokumentu konsorcia World Wide Web Consortium s názvem XML Schema Part 2: Datatypes.
| Datový typ XSD | Datový typ .NET |
|---|---|
| Anyuri | String |
| Base64binary | Byte Pole objektů |
| Boolean | Boolean |
| byte | SByte |
| date | DateTime |
| datum a čas | DateTime |
| desítkové číslo | Decimal |
| dvojitý | Double |
| ENTITY | String |
| ENTITY | String |
| float | Single |
| gDay | String |
| gMonth | String |
| gMonthDay | String |
| gYear | String |
| gYearMonth | String |
| Hexbinary | Byte Pole objektů |
| ID | String |
| IDREF | String |
| IDREFS | String |
| int | Int32 |
| integer | String |
| jazyk | String |
| long | Int64 |
| název | String |
| Ncname | String |
| negativeInteger | String |
| NMTOKEN | String |
| NMTOKENS | String |
| normalizedString | String |
| nonNegativeInteger | String |
| nonPositiveInteger | String |
| ZÁPIS | String |
| positiveInteger | String |
| QName | XmlQualifiedName |
| duration | String |
| řetězec | String |
| short | Int16 |
| time | DateTime |
| token | String |
| bezznaménkový bajt | Byte |
| neznačené celé číslo | UInt32 |
| unsignedLong | UInt64 |
| unsignedShort (nepodepsaný krátký) | UInt16 |