XmlAttributeAttribute クラス
アセンブリ: System.Xml (system.xml.dll 内)

<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)> _ Public Class XmlAttributeAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue)] public class XmlAttributeAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue)] public ref class XmlAttributeAttribute : public Attribute

XmlAttributeAttribute は、XmlSerializer がオブジェクトをシリアル化または逆シリアル化する方法を制御する一連の属性に属します。類似する属性の完全な一覧については、「XML シリアル化を制御する属性」を参照してください。
XmlAttributeAttribute は、パブリック フィールドまたはパブリック プロパティに適用された場合、それらのフィールドやプロパティを XML 属性としてシリアル化するように XmlSerializer に通知します。既定では、XmlSerializer は、パブリック フィールドまたはパブリック プロパティを XML 要素としてシリアル化します。
XmlAttributeAttribute は、XML スキーマ定義言語 (XSD) の単純型 (anySimpleType 型から派生した組み込みデータ型を含む) の 1 つに割り当てることができる値または値の配列を返す、パブリック フィールドまたはパブリック プロパティにだけ割り当てることができます。Guid、Char、列挙体など、XSD 単純型に割り当てることができるすべてのデータ型を使用できます。XSD 型のリスト、および .NET データ型への割り当て方法については、DataType プロパティのトピックを参照してください。
XmlAttributeAttribute と共に設定できる特殊な属性が 2 つあります。xml:lang (言語を指定) 属性および xml:space (空白の処理方法を指定) 属性です。これらの属性は、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 [Visual Basic] <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 の適用対象となる複数のフィールドを保持しているクラスをシリアル化する例を次に示します。
Option Explicit Option Strict Imports System 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
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(); } }
#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" ); }
import System.*; import System.IO.*; import System.Xml.*; import System.Xml.Serialization.*; import System.Xml.Schema.*; public class Group { /** @attribute XmlAttribute(Namespace = "http://www.cpandl.com") */ public String groupName; /** @attribute XmlAttribute(DataType = "base64Binary") */ public System.Byte groupNumber[]; /** @attribute XmlAttribute(DataType = "date", AttributeName = "CreationDate") */ public DateTime today; } //Group public class Run { public static void main(String[] args) { Run test = new Run(); test.SerializeObject("Attributes.xml"); } //main public void SerializeObject(String fileName) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = new XmlSerializer(Group.class.ToType()); // 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"; System.Byte hexByte[] = new System.Byte[] { (System.Byte)Convert.ToSByte(100) , (System.Byte)Convert.ToSByte(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(); } //SerializeObject } //Run

System.Attribute
System.Xml.Serialization.XmlAttributeAttribute


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- XmlAttributeAttribute クラスのページへのリンク