次の方法で共有


XmlElementAttribute.DataType プロパティ

XmlSerializer によって生成された XML 要素の XML スキーマ定義 (XSD: XML Schema Definition) データ型を取得または設定します。

Public Property DataType As String
[C#]
public string DataType {get; set;}
[C++]
public: __property String* get_DataType();public: __property void set_DataType(String*);
[JScript]
public function get DataType() : String;public function set DataType(String);

プロパティ値

W3C (World Wide Web Consortium) (www.w3.org) のドキュメント『XML Schema Part 2: Datatypes』で定義されている XML スキーマ データ型。

例外

例外の種類 条件
Exception 指定した XML スキーマ データ型を .NET データ型に割り当てることができません。

解説

XML スキーマの単純データ型と、それに相当する .NET データ型の表を次に示します。

XML スキーマの base64Binary データ型と hexBinary データ型については、 Byte 構造体の配列を使用し、 DataType をそれぞれ "base64Binary" または "hexBinary" に設定した XmlElementAttribute を適用します。XML スキーマの time データ型と date データ型については、 DateTime 型を使用して、 DataType を "date" または "time" に設定した XmlElementAttribute を適用します。

文字列に割り当てられるすべての XML スキーマ データ型については、 DataType プロパティをその XML スキーマ型に設定した XmlElementAttribute を適用します。これにより、メンバのスキーマだけが変更され、シリアル化形式は変更されなくなります。

メモ   このプロパティでは大文字と小文字が区別されるため、XML スキーマ データ型を正しく設定する必要があります。

メモ   バイナリ データは、XML スキーマ属性として渡すよりも XML 要素として渡す方が効率的です。

XML データ型の詳細については、W3C (World Wide Web Consortium) (www.w3.org) のドキュメント『XML Schema Part 2: Datatypes』を参照してください。

XSD データ型 .NET データ型
anyURI String
base64Binary Byte オブジェクトの配列
boolean Boolean
byte SByte
date DateTime
dateTime DateTime
decimal Decimal
double Double
ENTITY String
ENTITIES String
float Single
gDay String
gMonth String
gMonthDay String
gYear String
gYearMonth String
hexBinary Byte オブジェクトの配列
ID String
IDREF String
IDREFS String
int Int32
integer 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 String
short Int16
time DateTime
token String
unsignedByte Byte
unsignedInt UInt32
unsignedLong UInt64
unsignedShort UInt16

使用例

[Visual Basic, C#, C++] ExtraInfo という名前のフィールドがある Group という名前のクラスをシリアル化し、 ArrayList を返す例を次に示します。この例は、 XmlElementAttribute の 2 つのインスタンスをフィールドに適用し、それぞれのインスタンスに対して異なる DataType 値を指定します。各インスタンスは、配列に挿入されている指定した型を XmlSerializer でシリアル化できるようにします。

 
Imports System
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


[C#] 
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();
   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml::Serialization;

public __gc 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. */ 
public:
   [XmlElement(DataType = S"string",
      Type = __typeof(String),
      Namespace = S"http://www.cpandl.com"),
      XmlElement(DataType = S"int", 
      Namespace = S"http://www.cohowinery.com",
      Type = __typeof(Int32))]
   ArrayList* ExtraInfo;
};

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(S"hello");
   myGroup->ExtraInfo->Add(__box(100));

   // Serialize the object and close the TextWriter.
   s->Serialize(writer,myGroup);
   writer->Close();
}

int main()
{
   SerializeObject(S"ElementTypes.xml");
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlElementAttribute クラス | XmlElementAttribute メンバ | System.Xml.Serialization 名前空間