XmlAttributeAttribute クラス

定義

XmlSerializer がクラス メンバーを XML 属性としてシリアル化する必要があることを指定します。

public ref class XmlAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlAttributeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlAttributeAttribute = class
    inherit Attribute
Public Class XmlAttributeAttribute
Inherits Attribute
継承
XmlAttributeAttribute
属性

次の例では、 が適用される複数のフィールドを含むクラスを XmlAttributeAttribute シリアル化します。

#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:

   [XmlAttributeAttribute(Namespace="http://www.cpandl.com")]
   String^ GroupName;

   [XmlAttributeAttribute(DataType="base64Binary")]
   array<Byte>^GroupNumber;

   [XmlAttributeAttribute(DataType="date",AttributeName="CreationDate")]
   DateTime Today;
};

void SerializeObject( String^ filename )
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Group::typeid );

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew Group;

   // Set the object properties.
   myGroup->GroupName = ".NET";
   array<Byte>^hexByte = {Convert::ToByte( 100 ),Convert::ToByte( 50 )};
   myGroup->GroupNumber = hexByte;
   DateTime myDate = DateTime(2001,1,10);
   myGroup->Today = myDate;

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

int main()
{
   SerializeObject( "Attributes.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

public class Group
{
   [XmlAttribute (Namespace = "http://www.cpandl.com")]
   public string GroupName;

   [XmlAttribute(DataType = "base64Binary")]
   public Byte [] GroupNumber;

   [XmlAttribute(DataType = "date", AttributeName = "CreationDate")]
   public DateTime Today;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("Attributes.xml");
   }

   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Group));

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";

      Byte [] hexByte = new Byte[2]{Convert.ToByte(100),
      Convert.ToByte(50)};
      myGroup.GroupNumber = hexByte;

      DateTime myDate = new DateTime(2001,1,10);
      myGroup.Today = myDate;

      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
       writer.Close();
   }
}
Option Explicit
Option Strict

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


Public Class Group
    <XmlAttribute(Namespace := "http://www.cpandl.com")> _
        Public GroupName As String    
    <XmlAttribute(DataType := "base64Binary")> _
        Public GroupNumber() As Byte    
    <XmlAttribute(DataType := "date", AttributeName := "CreationDate")> _
        Public Today As DateTime
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Attributes.xml")
    End Sub 
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As New XmlSerializer(GetType(Group))
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the class that will be serialized.
        Dim myGroup As New Group()
        
        ' Set the object properties.
        myGroup.GroupName = ".NET"
        
        Dim hexByte() As Byte = {Convert.ToByte(100), Convert.ToByte(50)}
        myGroup.GroupNumber = hexByte
        
        Dim myDate As New DateTime(2001, 1, 10)
        myGroup.Today = myDate
        
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class

注釈

XmlAttributeAttribute 、オブジェクトをシリアル化または逆シリアル化する方法を XmlSerializer 制御する属性のファミリに属します。 類似する属性の完全な一覧については、「 XML シリアル化を制御する属性」を参照してください。

パブリック フィールドまたはプロパティに適用すると、 XmlAttributeAttribute は メンバーを XML 属性としてシリアル化するように を通知 XmlSerializer します。 既定では、 は XmlSerializer パブリック フィールドとプロパティを XML 要素としてシリアル化します。

XML スキーマ定義言語 (XSD) のいずれかの単純型 (XSD anySimpleType 型から派生したすべての組み込みデータ型を含む) にマップできる値 (または値の配列) を返すパブリック フィールドまたはパブリック プロパティにのみ を割り当てることができますXmlAttributeAttribute。 使用できる型には、XSD 単純型 (、、および 列挙型など) にマップできる が含GuidCharまれます。 XSD 型の DataType 一覧と、データ型 to.NET マップする方法については、 プロパティを参照してください。

(言語を指定) 属性と xml:space (空白の処理方法をXmlAttributeAttributexml:lang 指定する) 属性で設定できる 2 つの特別な属性があります。 これらの属性は、XML を処理するアプリケーションにのみ関連する情報を伝えることを目的としています。 これらを設定する例を次のコードに示します。

[XmlAttribute("xml:lang")]  
 public string Lang;  
 // Set this to 'default' or 'preserve'.  
 [XmlAttribute("space",   
 Namespace = "http://www.w3.org/XML/1998/namespace")]  
 public string Space 
<XmlAttribute("xml:lang")> _  
Public Lang As String   
' Set this to 'default' or 'preserve'.  
<XmlAttribute("space", _  
Namespace:= "http://www.w3.org/XML/1998/namespace")> _  
Public Space As String  

属性の使用の詳細については、「 属性」を参照してください。

注意

長い XmlAttributeAttributeではなく、コードで 単語XmlAttributeを使用できます。

コンストラクター

XmlAttributeAttribute()

XmlAttributeAttribute クラスの新しいインスタンスを初期化します。

XmlAttributeAttribute(String)

XmlAttributeAttribute クラスの新しいインスタンスを初期化し、生成される XML 属性の名前を指定します。

XmlAttributeAttribute(String, Type)

XmlAttributeAttribute クラスの新しいインスタンスを初期化します。

XmlAttributeAttribute(Type)

XmlAttributeAttribute クラスの新しいインスタンスを初期化します。

プロパティ

AttributeName

XML 属性の名前を取得または設定します。

DataType

XmlSerializer によって生成された XML 属性の XSD データ型を取得または設定します。

Form

XmlSerializer によって生成された XML 属性名が修飾されているかどうかを示す値を取得または設定します。

Namespace

XML 属性の XML 名前空間を取得または設定します。

Type

XML 属性の複合型を取得または設定します。

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象