XmlTextAttribute.DataType プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlTextAttribute.DataType プロパティの意味・解説 

XmlTextAttribute.DataType プロパティ

XmlSerializer によって生成されテキストXML スキーマ定義言語 (XSD: XML Schema Definition Language) データ型取得または設定します

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

例外例外
例外種類条件

Exception

指定した XML スキーマ データ型.NET データ型割り当てることはできません。

InvalidOperationException

指定した XML スキーマ データ型プロパティとしては無効なので、そのメンバ型に変換できません。

解説解説

DataType プロパティXML スキーマの単純データ型設定すると、生成される XML書式影響受けます。たとえば、このプロパティに "date" を設定すると、生成されるテキスト書式汎用的日付スタイル (2001-08-31 など) になります。それとは対照的に、このプロパティに "dateTime" を設定すると、ISO (International Organization for Standardization) のドキュメント 8601、『Representations of Dates and Times』で定義されているような、特定の瞬間を表す書式 (2001-08-15T06:59:11.0508456-07:00) になります

DataType プロパティ設定による影響は、XML スキーマ定義ツール (Xsd.exe) を使用してコンパイル済みファイル用の XML スキーマ生成する場合にも及びます。このツール使用方法については、「XML スキーマ定義ツールXML シリアル化」を参照してください

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

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

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

メモメモ

このプロパティでは大文字と小文字区別されるため、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

使用例使用例
Imports System
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
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();
   }   
}
#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();
}
import System.*;
import System.Xml.Serialization.*;
import System.IO.*;

public class Group1
{
    // The XmlTextAttribute with type set to string informs the 
    // XmlSerializer that strings should be serialized as XML text.
    /** @attribute XmlText(String.class)
     */
    /** @attribute XmlElement(int.class)
     */
    /** @attribute XmlElement(double.class)
     */
    public Object all[] = new Object[] { (Int32)321,
 "One", (Int32)2,
        (System.Double)3.0, "Two" };
} //Group1

public class Group2
{
    /** @attribute XmlElement(GroupType.class, ElementName = "GroupType")
     */
    public GroupType type;
} //Group2

public class GroupType
{
    private int member;
    GroupType()
    {
        member = 0;
    } //GroupType

    GroupType(int n)
    {
        member = n;
    } //GroupType

    public static int small;
    public static int medium;
    public static int large;
} //GroupType

public class Group3
{
    /** @attribute XmlText(Type = DateTime.class)
     */
    public DateTime creationTime = DateTime.get_Now();
} //Group3

public class Test
{
    public static void main(String[]
 args)
    {
        Test t = new Test();
        t.SerializeArray("XmlText1.xml");
        t.SerializeEnum("XmlText2.xml");
        t.SerializeDateTime("XmlText3.xml");
    } //main

    private void SerializeArray(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group1.class.ToType());
        Group1 myGroup1 = new Group1();
        TextWriter writer = new StreamWriter(fileName);
        ser.Serialize(writer, myGroup1);
        writer.Close();
    } //SerializeArray

    private void SerializeEnum(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group2.class.ToType());
        Group2 myGroup = new Group2();
        myGroup.type = new GroupType(GroupType.medium);
        TextWriter writer = new StreamWriter(fileName);
        ser.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeEnum

    private void SerializeDateTime(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group3.class.ToType());
        Group3 myGroup = new Group3();
        TextWriter writer = new StreamWriter(fileName);
        ser.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeDateTime
} //Test
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlTextAttribute クラス
XmlTextAttribute メンバ
System.Xml.Serialization 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

XmlTextAttribute.DataType プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlTextAttribute.DataType プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS