XmlTextAttribute.DataType 属性

定义

获取或设置由 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

属性值

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 架构base64BinaryhexBinary数据类型,请使用结构数组Byte,并根据需要将XmlTextAttributeDataType集应用于“base64Binary”或“hexBinary”。 对于 XML 架构timedate数据类型,请使用该DateTime类型并将集应用于XmlTextAttributeDataType“date”或“time”。

对于映射到字符串的每个 XML 架构数据类型,请将 XmlTextAttributeDataType 属性设置为 XML 架构数据类型。 请注意,这不会更改序列化格式,仅更改成员的架构。

备注

该属性区分大小写,因此必须将其完全设置为 XML 架构数据类型之一。

备注

将二进制数据作为 XML 元素传递比作为 XML 属性传递更有效。

有关 XML 架构数据类型的详细信息,请参阅名为 XML 架构第 2 部分的万维网联盟文档:数据类型。

XSD 数据类型 .NET 数据类型
anyURI String
base64Binary Byte 对象的数组
boolean Boolean
字节 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
令牌 String
unsignedByte Byte
unsignedInt UInt32
unsignedLong UInt64
unsignedShort UInt16

适用于