XmlTextAttributeとは? わかりやすく解説

XmlTextAttribute クラス

XmlSerializer が、クラスシリアル化または逆シリアル化するときに、そのクラス含まれる特定のメンバXML テキストとして処理する必要があることを指定します

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

<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field
 Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
 _
Public Class XmlTextAttribute
    Inherits Attribute
Dim instance As XmlTextAttribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue)]
 
public class XmlTextAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue)]
 
public ref class XmlTextAttribute : public
 Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue)
 */ 
public class XmlTextAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue)
 
public class XmlTextAttribute extends
 Attribute
解説解説

XmlTextAttribute は、XmlSerializerオブジェクトSerialize メソッドおよび Deserialize メソッド使用してシリアル化する方法制御する一連の属性のうちの 1 つです。類似する属性の完全な一覧については、「XML シリアル化制御する属性」を参照してください

1 つのクラス適用できるのは、XmlTextAttribute クラス1 つインスタンスだけです。

XmlTextAttribute は、パブリック フィールドおよびパブリック読み書き可能プロパティ適用できます

文字列配列返すフィールドまたはプロパティには、XmlTextAttribute適用できます。この属性Object 型の配列にも適用できますが、Type プロパティ文字列型設定する必要があります。この場合は、この配列挿入され任意の文字列XML テキストとしてシリアル化されます

XmlTextAttribute は、XmlNode を返すフィールドや、XmlNode オブジェクト配列返すフィールドにも適用できます

既定では、XmlSerializerクラス メンバXML 要素としてシリアル化ます。ただし、XmlTextAttributeメンバ適用すると、XmlSerializer はそのメンバの値を XML テキスト変換します。つまり、値はエンコードされて XML 要素内容となります

XML スキーマ定義ツール (Xsd.exe) は、XML スキーマ定義 (XSD: XML Schema Definition) ファイル (.xsd) からクラス作成するときに、XmlTextAttribute生成することがあります。これが生成されるのは、混合コンテンツを含む complexTypeスキーマ含まれている場合です。この場合は、対応するクラスにも文字列配列返すメンバ含まれることになり、そのメンバには XmlTextAttribute適用されます。たとえば、Xml Schema Definition ツール次のスキーマ処理するとします

 <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace=""
 
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="LinkList" type="LinkList" />
   <xs:complexType name="LinkList" mixed="true">
     <xs:sequence>
       <xs:element minOccurs="1" maxOccurs="1" name="id" type="xs:int" />
       <xs:element minOccurs="0" maxOccurs="1" name="name" type="xs:string" />
       <xs:element minOccurs="0" maxOccurs="1" name="next" type="LinkList" />
     </xs:sequence>
   </xs:complexType>
 </xs:schema>

次のクラス生成されます。

 ' Visual Basic code
 Public Class LinkList 
    Public id As Integer
    Public string name
    Public LinkList next
    <System.Xml.Serialization.XmlTextAttribute()> _
    Public Text() As string
 End Class
 // C# code
 public class LinkList {
    public int id;
    public string name;
    public LinkList next;
    [System.Xml.Serialization.XmlTextAttribute()]
    public string[] Text;
 }

属性使用方法については、「属性使用したメタデータ拡張」を参照してください

メモメモ

コードでは、XmlTextAttribute代わりに XmlText という短い語使用できます

使用例使用例
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
継承階層継承階層
System.Object
   System.Attribute
    System.Xml.Serialization.XmlTextAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlTextAttribute メンバ
System.Xml.Serialization 名前空間
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlSerializer クラス
XmlAttributes.XmlText プロパティ
XmlAttributes クラス
その他の技術情報
XML シリアル化概要
方法 : XML ストリーム代替要素名を指定する
属性使用した XML シリアル化制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)

XmlTextAttribute コンストラクタ ()

XmlTextAttribute クラス新しインスタンス初期化します。

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

Dim instance As New XmlTextAttribute
public XmlTextAttribute ()
public:
XmlTextAttribute ()
public XmlTextAttribute ()
public function XmlTextAttribute ()
解説解説
使用例使用例

Comment という名前のパブリック フィールドがあるクラスシリアル化する例を次に示します。この例では XmlTextAttributeフィールド適用することで、そのフィールドXML 要素としてシリアル化する代わりに XML テキストとしてシリアル化ます。

Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml.Schema
Imports System.Xml


Public Class Group
    Public GroupName As String
    Public Comment As String
End Class

Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializerOrder("TextOverride.xml")
    End Sub
    
    ' Create an instance of the XmlSerializer class that overrides
    ' the default way it serializes an object. 
    Public Function CreateOverrider() As
 XmlSerializer
        ' CreatE instances of the XmlAttributes and
        ' XmlAttributeOverrides classes.
        
        Dim attrs As New
 XmlAttributes()        
        Dim xOver As New
 XmlAttributeOverrides()
        
        ' Create an XmlTextAttribute to override the default
        ' serialization process. 
        Dim xText As New
 XmlTextAttribute()
        attrs.XmlText = xText
        
        ' Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(GetType(Group), "Comment",
 attrs)
        
        ' Create the XmlSerializer, and return it.
        Dim xSer As New
 XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializerOrder(ByVal
 filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object and serialize it.
        Dim myGroup As New
 Group()
        myGroup.Comment = "This is a great product."
        
        Dim writer As New
 StreamWriter(filename)
        xSer.Serialize(writer, myGroup)
    End Sub
End Class

using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;


public class Group {
    public string GroupName;
    public string Comment;
}

public class Test {
    public static void Main()
 {
        Test t = new Test();
        t.SerializerOrder("TextOverride.xml");
    }
    /* Create an instance of the XmlSerializer class that overrides
 
       the default way it serializes an object. */
    public XmlSerializer CreateOverrider() {
        /* Create instances of the XmlAttributes and 
        XmlAttributeOverrides classes. */
   
        XmlAttributes attrs = new XmlAttributes();

        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
              
        /* Create an XmlTextAttribute to override the default
 
        serialization process. */
        XmlTextAttribute xText = new XmlTextAttribute();
        attrs.XmlText = xText;

        // Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(typeof(Group), "Comment", attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer = new XmlSerializer(typeof(Group),
 xOver);
        return xSer;
    }

    public void SerializerOrder(string
 filename) {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Group myGroup = new Group();
        myGroup.Comment = "This is a great product.";
      
        TextWriter writer = new StreamWriter(filename);
        xSer.Serialize(writer, myGroup);
    }
}
   
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::Xml::Schema;

public ref class Group
{
public:
   String^ GroupName;
   String^ Comment;
};

public ref class Test
{
public:
   static void main()
   {
      Test^ t = gcnew Test;
      t->SerializerOrder( "TextOverride.xml" );
   }

   /* Create an instance of the XmlSerializer class that overrides
 
          the default way it serializes an object. */
   XmlSerializer^ CreateOverrider()
   {
      /* Create instances of the XmlAttributes and 
              XmlAttributeOverrides classes. */
      XmlAttributes^ attrs = gcnew XmlAttributes;
      XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;

      /* Create an XmlTextAttribute to override the default 
              serialization process. */
      XmlTextAttribute^ xText = gcnew XmlTextAttribute;
      attrs->XmlText = xText;

      // Add the XmlAttributes to the XmlAttributeOverrides.
      xOver->Add( Group::typeid, "Comment", attrs );

      // Create the XmlSerializer, and return it.
      XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
      return xSer;
   }

   void SerializerOrder( String^ filename )
   {
      // Create an XmlSerializer instance.
      XmlSerializer^ xSer = CreateOverrider();

      // Create the object and serialize it.
      Group^ myGroup = gcnew Group;
      myGroup->Comment = "This is a great product.";
      TextWriter^ writer = gcnew StreamWriter( filename );
      xSer->Serialize( writer, myGroup );
   }
};

int main()
{
   Test::main();
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.Schema.*;
import System.Xml.*;

public class Group
{
    public String groupName;
    public String comment;
} //Group

public class Test
{
    public static void main(String[]
 args)
    {
        Test t = new Test();
        t.SerializerOrder("TextOverride.xml");
    } //main

    /* Create an instance of the XmlSerializer class that overrides
 
       the default way it serializes an object. */
    public XmlSerializer CreateOverrider()
    {
        /* Create instances of the XmlAttributes and 
           XmlAttributeOverrides classes. */
        XmlAttributes attrs = new XmlAttributes();
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();

        /* Create an XmlTextAttribute to override the default
 
           serialization process. */
        XmlTextAttribute xText = new XmlTextAttribute();
        attrs.set_XmlText(xText);

        // Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(Group.class.ToType(), "comment", attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer = new XmlSerializer(Group.class.ToType(),
 xOver);
        return xSer;
    } //CreateOverrider

    public void SerializerOrder(String filename)
    {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Group myGroup = new Group();
        myGroup.comment = "This is a great product.";
        TextWriter writer = new StreamWriter(filename);
        xSer.Serialize(writer, myGroup);
    } //SerializerOrder
} //Test
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlTextAttribute クラス
XmlTextAttribute メンバ
System.Xml.Serialization 名前空間

XmlTextAttribute コンストラクタ (Type)

XmlTextAttribute クラス新しインスタンス初期化します。

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

Dim type As Type

Dim instance As New XmlTextAttribute(type)
public XmlTextAttribute (
    Type type
)
public:
XmlTextAttribute (
    Type^ type
)
public XmlTextAttribute (
    Type type
)
public function XmlTextAttribute (
    type : Type
)

パラメータ

type

シリアル化するメンバType

解説解説
使用例使用例
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 名前空間

XmlTextAttribute コンストラクタ

XmlTextAttribute クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

参照参照

関連項目

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

XmlTextAttribute プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ .NET Compact Framework によるサポート .NET Compact Framework によるサポート TypeId  派生クラス実装されている場合は、この Attribute一意識別子取得します。 ( Attribute から継承されます。)
参照参照

関連項目

XmlTextAttribute クラス
System.Xml.Serialization 名前空間
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlSerializer クラス
XmlAttributes.XmlText プロパティ
XmlAttributes クラス

その他の技術情報

XML シリアル化概要
方法 : XML ストリーム代替要素名を指定する
属性使用した XML シリアル化制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)

XmlTextAttribute メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド参照)
  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用され指定した型のカスタム属性取得します。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されカスタム属性配列取得します。 ( Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスハッシュ コード返します。 ( Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 ( Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラス既定値かどうか示します。 ( Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます指定した型のカスタム属性が、アセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されているかどうか判断します。 ( Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンス等しかどうかを示す値を返します。 ( Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

XmlTextAttribute クラス
System.Xml.Serialization 名前空間
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlSerializer クラス
XmlAttributes.XmlText プロパティ
XmlAttributes クラス

その他の技術情報

XML シリアル化概要
方法 : XML ストリーム代替要素名を指定する
属性使用した XML シリアル化制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)

XmlTextAttribute メンバ

XmlSerializer が、クラスシリアル化または逆シリアル化するときに、そのクラス含まれる特定のメンバXML テキストとして処理する必要があることを指定します

XmlTextAttribute データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ .NET Compact Framework によるサポート .NET Compact Framework によるサポート TypeId  派生クラス実装されている場合は、この Attribute一意識別子取得します。(Attribute から継承されます。)
パブリック メソッドパブリック メソッド
( プロテクト メソッド参照)
  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用され指定した型のカスタム属性取得します。 (Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされますアセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されカスタム属性配列取得します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスハッシュ コード返します。 (Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラス既定値かどうか示します。 (Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます指定した型のカスタム属性が、アセンブリモジュール、型のメンバ、またはメソッド パラメータ適用されているかどうか判断します。 (Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンス等しかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

XmlTextAttribute クラス
System.Xml.Serialization 名前空間
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlSerializer クラス
XmlAttributes.XmlText プロパティ
XmlAttributes クラス

その他の技術情報

XML シリアル化概要
方法 : XML ストリーム代替要素名を指定する
属性使用した XML シリアル化制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)



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

辞書ショートカット

すべての辞書の索引

「XmlTextAttribute」の関連用語

XmlTextAttributeのお隣キーワード
検索ランキング

   

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



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

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

©2024 GRAS Group, Inc.RSS