XmlTextAttribute.DataType 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 XmlSerializer 所產生之文字的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 架構 (XSD) 資料類型。
例外狀況
您指定的 XML 結構描述資料型別無法對應至 .NET 資料型別。
您指定的 XML 結構描述資料型別對於該屬性無效,且無法轉換為成員型別。
範例
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::Xml::Serialization;
using namespace System::IO;
public ref class Group1
{
public:
// The XmlTextAttribute with type set to string informs the
// XmlSerializer that strings should be serialized as XML text.
[XmlText(String::typeid)]
[XmlElement(Int32::typeid)]
[XmlElement(Double::typeid)]
array<Object^>^All;
Group1()
{
array<Object^>^temp = {321,"One",2,3.0,"Two"};
All = temp;
}
};
public enum class GroupType
{
Small, Medium, Large
};
public ref class Group2
{
public:
[XmlText(Type=GroupType::typeid)]
GroupType Type;
};
public ref class Group3
{
public:
[XmlText(Type=DateTime::typeid)]
DateTime CreationTime;
Group3()
{
CreationTime = DateTime::Now;
}
};
public ref class Test
{
public:
static void main()
{
Test^ t = gcnew Test;
t->SerializeArray( "XmlText1.xml" );
t->SerializeEnum( "XmlText2.xml" );
t->SerializeDateTime( "XmlText3.xml" );
}
private:
void SerializeArray( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group1::typeid );
Group1^ myGroup1 = gcnew Group1;
TextWriter^ writer = gcnew StreamWriter( filename );
ser->Serialize( writer, myGroup1 );
writer->Close();
}
void SerializeEnum( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group2::typeid );
Group2^ myGroup = gcnew Group2;
myGroup->Type = GroupType::Medium;
TextWriter^ writer = gcnew StreamWriter( filename );
ser->Serialize( writer, myGroup );
writer->Close();
}
void SerializeDateTime( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group3::typeid );
Group3^ myGroup = gcnew Group3;
TextWriter^ writer = gcnew StreamWriter( filename );
ser->Serialize( writer, myGroup );
writer->Close();
}
};
int main()
{
Test::main();
}
using System;
using System.Xml.Serialization;
using System.IO;
public class Group1{
// The XmlTextAttribute with type set to string informs the
// XmlSerializer that strings should be serialized as XML text.
[XmlText(typeof(string))]
[XmlElement(typeof(int))]
[XmlElement(typeof(double))]
public object [] All= new object []{321, "One", 2, 3.0, "Two" };
}
public class Group2{
[XmlText(Type = typeof(GroupType))]
public GroupType Type;
}
public enum GroupType{
Small,
Medium,
Large
}
public class Group3{
[XmlText(Type=typeof(DateTime))]
public DateTime CreationTime = DateTime.Now;
}
public class Test{
static void Main(){
Test t = new Test();
t.SerializeArray("XmlText1.xml");
t.SerializeEnum("XmlText2.xml");
t.SerializeDateTime("XmlText3.xml");
}
private void SerializeArray(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group1));
Group1 myGroup1 = new Group1();
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, myGroup1);
writer.Close();
}
private void SerializeEnum(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group2));
Group2 myGroup = new Group2();
myGroup.Type = GroupType.Medium;
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, myGroup);
writer.Close();
}
private void SerializeDateTime(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group3));
Group3 myGroup = new Group3();
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, myGroup);
writer.Close();
}
}
Imports System.Xml.Serialization
Imports System.IO
Public Class Group1
' The XmlTextAttribute with type set to String informs the
' XmlSerializer that strings should be serialized as XML text.
<XmlText(GetType(String)), _
XmlElement(GetType(integer)), _
XmlElement(GetType(double))> _
public All () As Object = _
New Object (){321, "One", 2, 3.0, "Two" }
End Class
Public Class Group2
<XmlText(GetType(GroupType))> _
public Type As GroupType
End Class
Public Enum GroupType
Small
Medium
Large
End Enum
Public Class Group3
<XmlText(GetType(DateTime))> _
Public CreationTime As DateTime = DateTime.Now
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
t.SerializeArray("XmlText1.xml")
t.SerializeEnum("XmlText2.xml")
t.SerializeDateTime("XmlText3.xml")
End Sub
Private Sub SerializeArray(filename As String)
Dim ser As XmlSerializer = New XmlSerializer(GetType(Group1))
Dim myGroup1 As Group1 = New Group1()
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, myGroup1)
writer.Close()
End Sub
Private Sub SerializeEnum(filename As String)
Dim ser As XmlSerializer = New XmlSerializer(GetType(Group2))
Dim myGroup As Group2 = New Group2()
myGroup.Type = GroupType.Medium
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, myGroup)
writer.Close()
End Sub
Private Sub SerializeDateTime(filename As String)
Dim ser As XmlSerializer = new XmlSerializer(GetType(Group3))
Dim myGroup As Group3 = new Group3()
Dim writer As TextWriter = new StreamWriter(filename)
ser.Serialize(writer, myGroup)
writer.Close()
End Sub
End Class
備註
將 DataType 屬性設定為 XML 架構單一資料型別會影響所產生 XML 的格式。 例如,將 屬性設定為 「date」 會導致產生的文字格式化為一般日期樣式,例如:2001-08-31。 相反地,將 屬性設定為 「dateTime」 會產生國際標準組織檔 8601 所定義的特定立即,例如:2001-08-15T06:59:11.0508456-07:00。
使用XML 架構定義工具 (Xsd.exe) 來產生已編譯檔案的 XML 架構時,也可以看到設定 DataType 屬性的效果。 如需使用工具的詳細資訊,請參閱 XML 架構定義工具和 XML 序列化。
下表列出 XML 架構單一資料型別及其 .NET 對等專案。
對於 XML 架構 base64Binary
和資料類型,請使用 結構的陣列 Byte ,並視情況將 設定為 「base64Binary」 或 「hexBinary」 套用 XmlTextAttributeDataType 。 hexBinary
對於 XML 架構 time
和資料類型,請使用 DateTime 型別,並將 設定為 「date」 或 「time」 的 套用 XmlTextAttributeDataType 。 date
針對對應至字串的每個 XML 架構資料類型,套用 XmlTextAttribute 其 DataType 屬性設定為 XML 架構資料類型的 。 請注意,這不會變更序列化格式,只會變更成員的架構。
注意
屬性會區分大小寫,因此您必須將它完全設定為其中一個 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 |