英語で読む

次の方法で共有


XmlAttributes クラス

定義

XmlSerializer がオブジェクトをシリアル化および逆シリアル化する方法を制御する属性オブジェクトのコレクションを表します。

C#
public class XmlAttributes
継承
XmlAttributes

次の例では、 という名前 Orchestraのクラスのインスタンスをシリアル化します。このインスタンスには、 オブジェクトの配列を返す という名前 InstrumentsInstrument フィールドが 1 つ含まれています。 という名前 Brass の 2 番目のクラスは、 Instrument クラスから継承します。 この例では、フィールドをXmlAttributesオーバーライドするオブジェクトをInstrument作成します。フィールドがオブジェクトを受け入れることBrassを許可し、オブジェクトを クラスのインスタンスにXmlAttributeOverrides追加XmlAttributesします。

C#
using System;
using System.IO;
using System.Xml.Serialization;

public class Orchestra
{
   public Instrument[] Instruments;
}

public class Instrument
{
   public string Name;
}

public class Brass:Instrument
{
   public bool IsValved;
}

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

    public void SerializeObject(string filename)
    {
      /* Each overridden field, property, or type requires
      an XmlAttributes object. */
      XmlAttributes attrs = new XmlAttributes();

      /* Create an XmlElementAttribute to override the
      field that returns Instrument objects. The overridden field
      returns Brass objects instead. */
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the element to the collection of elements.
      attrs.XmlElements.Add(attr);

      // Create the XmlAttributeOverrides object.
      XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();

      /* Add the type of the class that contains the overridden
      member and the XmlAttributes to override it with to the
      XmlAttributeOverrides object. */
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Create the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

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

      // Create the object that will be serialized.
      Orchestra band = new Orchestra();

      // Create an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;

      // Serialize the object.
      s.Serialize(writer,band);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlAttributeOverrides attrOverrides =
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Create an XmlElementAttribute to override the Instrument.
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the element to the collection of elements.
      attrs.XmlElements.Add(attr);

      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Create the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      FileStream fs = new FileStream(filename, FileMode.Open);
      Orchestra band = (Orchestra) s.Deserialize(fs);
      Console.WriteLine("Brass:");

      /* The difference between deserializing the overridden
      XML document and serializing it is this: To read the derived
      object values, you must declare an object of the derived type
      (Brass), and cast the Instrument instance to it. */
      Brass b;
      foreach(Instrument i in band.Instruments)
      {
         b = (Brass)i;
         Console.WriteLine(
         b.Name + "\n" +
         b.IsValved);
      }
   }
}

注釈

を作成することは XmlAttributes 、クラス インスタンスをシリアル化する既定の方法をオーバーライドする XmlSerializer プロセスの一部です。 たとえば、アクセスできないソースを持つ DLL から作成されたオブジェクトをシリアル化するとします。 を使用すると、 XmlAttributeOverridesオブジェクトのシリアル化方法を拡張または制御できます。

クラスのメンバーは、 XmlAttributes シリアル化を制御する属性クラスのファミリに直接対応します。 たとえば、 プロパティを XmlText に設定する XmlTextAttribute必要があります。これにより、 プロパティ値を XML テキストとしてシリアル化するように を指示 XmlSerializer することで、フィールドまたはプロパティのシリアル化をオーバーライドできます。 シリアル化を制御する属性の完全な一覧については、 を XmlSerializer参照してください。

クラスで を使用する方法の詳細については、「方法: XML ストリームの代替要素名を指定する」を参照してください。XmlAttributeOverridesXmlAttributes

コンストラクター

XmlAttributes()

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

XmlAttributes(ICustomAttributeProvider)

XmlAttributes クラスの新しいインスタンスを初期化し、XmlSerializer がオブジェクトをシリアル化および逆シリアル化する方法をカスタマイズします。

プロパティ

XmlAnyAttribute

オーバーライドする XmlAnyAttributeAttribute を取得または設定します。

XmlAnyElements

オーバーライドする XmlAnyElementAttribute オブジェクトのコレクションを取得します。

XmlArray

XmlSerializer が、配列を返すパブリック フィールドまたは読み取り/書き込みプロパティをシリアル化する方法を指定するオブジェクトを取得または設定します。

XmlArrayItems

パブリック フィールドまたは読み取り/書き込みプロパティによって返された配列に挿入されている項目を XmlSerializer によってシリアル化する方法を指定するオブジェクトのコレクションを取得または設定します。

XmlAttribute

XmlSerializer が、パブリック フィールドまたはパブリックな読み取り/書き込みプロパティを XML 属性としてシリアル化する方法を指定するオブジェクトを取得または設定します。

XmlChoiceIdentifier

複数の選択肢を区別できるようにするオブジェクトを取得または設定します。

XmlDefaultValue

XML 要素または XML 属性の既定値を取得または設定します。

XmlElements

XmlSerializer がパブリック フィールドまたは読み取り/書き込みプロパティを XML 要素としてシリアル化する方法を指定する、オブジェクトのコレクションを取得します。

XmlEnum

XmlSerializer が列挙体メンバーをシリアル化する方法を指定するオブジェクトを取得または指定します。

XmlIgnore

XmlSerializer がパブリック フィールドまたは読み書き可能なパブリック プロパティをシリアル化するかどうかを指定する値を取得または設定します。

Xmlns

XmlSerializerNamespaces オブジェクトを返すメンバーを格納するオブジェクトがオーバーライドされたときに、すべての名前空間宣言を保持するかどうかを示す値を取得または設定します。

XmlRoot

XmlSerializer がクラスを XML ルート要素としてシリアル化する方法を指定するオブジェクトを取得または指定します。

XmlText

XmlSerializer に対してパブリック フィールドまたはパブリックな読み取り/書き込みプロパティを XML テキストとしてシリアル化するよう指示するオブジェクトを取得または設定します。

XmlType

XmlSerializer が適用されているクラスを XmlTypeAttribute がシリアル化する方法を指定するオブジェクトを取得または指定します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
ToString()

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

(継承元 Object)

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

こちらもご覧ください