XmlElementAttribute.DataType 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 XmlSerializer 所產生 XML 項目的 XML 結構描述定義 (XSD) 資料型別。
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
屬性值
XML 架構資料類型。
例外狀況
您指定的 XML 結構描述資料型別無法對應至 .NET 資料型別。
範例
下列範例序列化名為 Group
的類別,其中包含名為 ExtraInfo
的欄位,其會傳 ArrayList 回 。 此範例會將 的 XmlElementAttribute 兩個實例套用至 欄位,並為每個實例指定不同的 DataType 值。 每個實例可讓 XmlSerializer 序列化插入陣列中的指定型別。
#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
備註
下表列出具有 their.NET 對等專案的 XML 架構單一資料型別。
針對 XML 架構 base64Binary
和資料類型,請使用結構的陣列 Byte ,並視需要將 設定為 「base64Binary」 或 「hexBinary」 套用 XmlElementAttribute DataType 。 hexBinary
針對 XML 架構 time
和 date
資料類型,請使用 DateTime 型別,並將 套 DataType 用 XmlElementAttribute 為 「date」 或 「time」。
對於對應至字串的每個 XML 架構類型,請將 其 DataType 屬性設定為 XML 架構類型套用 XmlElementAttribute 。 這可能會變更序列化格式,而不只是成員的架構。
注意
屬性會區分大小寫,因此您必須將其設定為其中一個 XML 架構資料類型。
注意
將二進位資料當做 XML 元素傳遞比將它當作 XML 架構屬性傳遞更有效率。
如需 XML 資料類型的詳細資訊,請參閱名為 XML 架構第 2 部分:資料類型的萬維網聯盟檔。
XSD 資料類型 | .NET 資料類型 |
---|---|
anyURI | String |
base64Binary | Byte物件的陣列 |
boolean | Boolean |
byte | SByte |
日期 | DateTime |
dateTime | DateTime |
decimal | Decimal |
double | Double |
ENTITY | String |
實體 | String |
FLOAT | Single |
gDay | String |
gMonth | String |
gMonthDay | String |
gYear | String |
gYearMonth | String |
hexBinary | Byte物件的陣列 |
ID | String |
IDREF | String |
IDREFS | String |
int | Int32 |
整數 | String |
語言 | String |
long | Int64 |
名稱 | String |
NCName | String |
negativeInteger | String |
NMTOKEN | String |
NMTOKENS | String |
normalizedString | String |
nonNegativeInteger | String |
nonPositiveInteger | String |
NOTATION | String |
positiveInteger | String |
QName | XmlQualifiedName |
duration | String |
字串 | String |
short | Int16 |
time | DateTime |
token | String |
unsignedByte | Byte |
unsignedInt | UInt32 |
unsignedLong | UInt64 |
unsignedShort | UInt16 |