XmlElementAttribute.DataType 속성

정의

XmlSerializer에 의해 생성된 XML 요소의 XSD(XML 스키마 정의) 데이터 형식을 가져오거나 설정합니다.

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 스키마 데이터 형식입니다.

예외

지정한 XML 스키마 데이터 형식을 .NET 데이터 형식에 매핑할 수 없는 경우

예제

다음 예제에서는 명명ExtraInfo된 필드가 들어 있는 클래스 Group 를 serialize합니다. 이 클래스는 을 반환합니다ArrayList. 이 예제에서는 필드에 두 개의 인스턴스를 XmlElementAttribute 적용하고 각 인스턴스에 대해 서로 다른 DataType 값을 지정합니다. 각 인스턴스를 사용하면 배열에 XmlSerializer 삽입된 지정된 형식을 serialize할 수 있습니다.

#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 스키마 base64BinaryhexBinary 데이터 형식의 경우 구조 배열 Byte 을 사용하고 집합을 DataType "base64Binary" 또는 "hexBinary"에 적절하게 적용 XmlElementAttribute 합니다. XML 스키마 timedate 데이터 형식의 경우 형식을 DateTime 사용하고 집합을 XmlElementAttribute DataType "date" 또는 "time"에 적용합니다.

문자열에 매핑되는 모든 XML 스키마 형식에 대해 해당 DataType 속성 집합을 XmlElementAttribute XML 스키마 형식에 적용합니다. 멤버에 대한 스키마뿐만 아니라 serialization 형식도 변경할 수 있습니다.

참고

속성은 대/소문자를 구분하므로 정확히 XML 스키마 데이터 형식 중 하나로 설정해야 합니다.

참고

이진 데이터를 XML 요소로 전달하는 것이 XML 스키마 특성으로 전달하는 것보다 더 효율적입니다.

XML 데이터 형식에 대한 자세한 내용은 XML 스키마 2부: Datatypes라는 World Wide Web 컨소시엄 문서를 참조하세요.

XSD 데이터 형식 .NET 데이터 형식
anyURI String
base64Binary Byte 개체의 배열
boolean Boolean
byte SByte
날짜 DateTime
dateTime DateTime
decimal Decimal
double Double
엔터티 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
language String
long Int64
Name 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

적용 대상