XmlElementAttribute.DataType 属性

定义

获取或设置由 XmlSerializer 生成的 XMl 元素的 XML 架构定义 (XSD) 数据类型。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

属性值

String

XML 架构数据类型。An XML Schema data type.

例外

已指定的 XML 架构数据类型无法映射到 .NET 数据类型。The XML Schema data type you have specified cannot be mapped to the.NET data type.

示例

下面的示例序列化一个名为的类,该类 Group 包含一个名为的字段 ExtraInfo ,该字段返回 ArrayListThe following example serializes a class named Group that contains a field named ExtraInfo, which returns an ArrayList. 该示例将的两个实例应用于 XmlElementAttribute 字段,并 DataType 为每个实例指定不同的值。The example applies two instances of the XmlElementAttribute to the field and specifies different DataType values for each instance. 每个实例都允许 XmlSerializer 对插入到数组中的指定类型进行序列化。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

注解

下表列出了具有 their.NET 等效项的 XML 架构简单数据类型。The following table lists the XML Schema simple data types with their.NET equivalents.

对于 XML 架构 base64BinaryhexBinary 数据类型,请使用结构的数组 Byte ,并 XmlElementAttribute DataType 根据需要将设置为 "base64Binary" 或 "hexBinary" 的。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. 对于 XML 架构 timedate 数据类型,请使用 DateTime 类型,并将 XmlElementAttribute DataType 设置为 "日期" 或 "时间"。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".

对于映射到字符串的每个 XML 架构类型,应用 XmlElementAttributeDataType 属性设置为 XML 架构类型的。For every XML Schema type that is mapped to a string, apply the XmlElementAttribute with its DataType property set to the XML Schema type. 这可能会更改序列化格式,而不仅是成员的架构。It is possible that this can change the serialization format, not only the schema for the member.

备注

属性区分大小写,因此必须将其精确设置为 XML 架构数据类型之一。The property is case-sensitive, so you must set it exactly to one of the XML Schema data types.

备注

将二进制数据作为 XML 元素传递比将其作为 XML 架构特性传递更为有效。Passing binary data as an XML element is more efficient than passing it as an XML Schema attribute.

有关 XML 数据类型的详细信息,请参阅名为 XML Schema 的万维网联合会文档 第2部分:数据类型For more information about XML data types, see the World Wide Web Consortium document named XML Schema Part 2: Datatypes.

XSD 数据类型XSD data type .NET 数据类型.NET data type
anyURIanyURI String
base64Binarybase64Binary 对象数组 ByteArray of Byte objects
booleanboolean Boolean
字节byte SByte
日期date DateTime
dateTimedateTime DateTime
Decimaldecimal Decimal
Doubledouble Double
ENTITYENTITY String
条目ENTITIES String
FLOATfloat Single
gDaygDay String
gMonthgMonth String
gMonthDaygMonthDay String
gYeargYear String
gYearMonthgYearMonth String
hexBinaryhexBinary 对象数组 ByteArray of Byte objects
IDID String
IDREFIDREF String
IDREFSIDREFS String
intint Int32
整型integer String
语言language String
longlong Int64
“属性”Name String
NCNameNCName String
negativeIntegernegativeInteger String
NMTOKENNMTOKEN String
NMTOKENSNMTOKENS String
normalizedStringnormalizedString String
nonNegativeIntegernonNegativeInteger String
nonPositiveIntegernonPositiveInteger String
NOTATIONNOTATION String
positiveIntegerpositiveInteger String
QNameQName XmlQualifiedName
durationduration String
字符串string String
shortshort Int16
timetime DateTime
令牌token String
unsignedByteunsignedByte Byte
unsignedIntunsignedInt UInt32
unsignedLongunsignedLong UInt64
unsignedShortunsignedShort UInt16

适用于