XmlElementAttribute コンストラクタとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlElementAttribute コンストラクタの意味・解説 

XmlElementAttribute コンストラクタ (String, Type)

XmlElementAttribute の新しインスタンス初期化しXmlElementAttribute適用先であるメンバXML 要素の名前と派生型指定します。このメンバ型が使用されるのは、その型を含むオブジェクトを XmlSerializer がシリアル化する場合です。

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

Public Sub New ( _
    elementName As String, _
    type As Type _
)
Dim elementName As String
Dim type As Type

Dim instance As New XmlElementAttribute(elementName,
 type)
public XmlElementAttribute (
    string elementName,
    Type type
)
public:
XmlElementAttribute (
    String^ elementName, 
    Type^ type
)
public XmlElementAttribute (
    String elementName, 
    Type type
)
public function XmlElementAttribute (
    elementName : String, 
    type : Type
)

パラメータ

elementName

シリアル化されたメンバXML 要素名。

type

メンバの型から派生したオブジェクトType

解説解説

既定では、XmlSerializerクラスインスタンスシリアル化するときは、メンバ名が XML 要素名として使用されます。たとえば、Vehicle という名前のフィールドは、Vehicle という名前の XML 要素生成します。ただし、Cars などの別の名前の要素必要な場合には、その名前を elementName パラメータ渡します

type パラメータ使用して基本クラスから派生する型を指定します。たとえば、MyAnimal という名前のプロパティAnimal オブジェクト返す場合想定します。このオブジェクト拡張するには、Mammal という名前の新しクラスAnimal クラスから継承して作成しますXmlSerializer に対してMyAnimal プロパティシリアル化するときに Mammal クラス受け入れるように指示するには、Mammal クラスTypeコンストラクタ渡します

使用例使用例

Instrument オブジェクト配列返すフィールド Instruments1 つ含んでいるクラス Orchestraシリアル化する例を次に示しますBrass という名前の 2 番目のクラスInstrument クラスから継承されます。この例では、XmlElementAttributeInstruments フィールド適用されBrass 型が指定されています。これにより、Instruments フィールドBrass オブジェクト受け入れるようになりますまた、ElementName プロパティ設定することによって、XML 要素の名前も指定します

Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic


Public Class Orchestra
    Public Instruments() As Instrument
End Class

Public Class Instrument
    Public Name As String
End Class

Public Class Brass
    Inherits Instrument
    Public IsValved As Boolean
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.SerializeObject("Override.xml")
        test.DeserializeObject("Override.xml")
    End Sub 'Main
    
    
    Public Sub SerializeObject(filename As
 String)
        ' To write the file, a TextWriter is required.
        Dim writer As New
 StreamWriter(filename)
        
        Dim attrOverrides As New
 XmlAttributeOverrides()
        Dim attrs As New
 XmlAttributes()
        
        ' Creates an XmlElementAttribute that overrides the Instrument
 type.
        Dim attr As New
 XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Adds the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments",
 attrs)
        
        ' Creates the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra),
 attrOverrides)
        
        ' Creates the object to serialize.
        Dim band As New
 Orchestra()
        
        ' Creates an object of the derived type.
        Dim i As New Brass()
        i.Name = "Trumpet"
        i.IsValved = True
        Dim myInstruments() As Instrument =
 {i}
        band.Instruments = myInstruments
        s.Serialize(writer, band)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(filename As
 String)
        Dim attrOverrides As New
 XmlAttributeOverrides()
        Dim attrs As New
 XmlAttributes()
        
        ' Create an XmlElementAttribute that override the Instrument
 type.
        Dim attr As New
 XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Add the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments",
 attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra),
 attrOverrides)
        
        Dim fs As New FileStream(filename,
 FileMode.Open)
        Dim band As Orchestra = CType(s.Deserialize(fs),
 Orchestra)
        Console.WriteLine("Brass:")
        
        ' Deserializing differs from serializing. To read the
        ' derived-object values, declare an object of the derived
        ' type (Brass) and cast the Instrument instance to it. 
        Dim b As Brass
        Dim i As Instrument
        For Each i In  band.Instruments
            b = CType(i, Brass)
            Console.WriteLine((b.Name + ControlChars.Cr + b.IsValved.ToString()))
        Next i
    End Sub
End Class

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)
    {
      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);
      
      XmlAttributeOverrides attrOverrides = 
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that overrides the Instrument
 type.
      XmlElementAttribute attr = new 
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

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

      // Creates the object to serialize.
      Orchestra band = new Orchestra();
      
      // Creates an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;
      s.Serialize(writer,band);
      writer.Close();
   }

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

      // Creates an XmlElementAttribute that override the Instrument
 type.
      XmlElementAttribute attr = new 
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates 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:");

      /* Deserializing differs from serializing. To read the 
         derived-object values, 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);
      }
   }
}

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Instrument
{
public:
   String^ Name;
};

public ref class Brass: public
 Instrument
{
public:
   bool IsValved;
};

public ref class Orchestra
{
public:
   array<Instrument^>^Instruments;
};

void SerializeObject( String^ filename )
{
   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that overrides the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );

   // Creates the object to serialize.
   Orchestra^ band = gcnew Orchestra;

   // Creates an object of the derived type.
   Brass^ i = gcnew Brass;
   i->Name = "Trumpet";
   i->IsValved = true;
   array<Instrument^>^myInstruments = {i};
   band->Instruments = myInstruments;
   s->Serialize( writer, band );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that override the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs ));
   Console::WriteLine( "Brass:" );

   /* Deserializing differs from serializing. To read the 
      derived-object values, declare an object of the derived 
      type (Brass) and cast the Instrument instance to it. */
   Brass^ b;
   System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Instrument^ i = safe_cast<Instrument^>(myEnum->Current);
      b = dynamic_cast<Brass^>(i);
      Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved );
   }
}

int main()
{
   SerializeObject( "Override.xml" );
   DeserializeObject( "Override.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;

public class Orchestra
{
    public Instrument instruments[];
} //Orchestra

public class Instrument
{
    public String name;
} //Instrument

public class Brass extends Instrument
{
    public boolean isValved;
} //Brass

public class Run
{
    public static void main(String[]
 args)
    {
        Run test = new Run();
        test.SerializeObject("Override.xml");
        test.DeserializeObject("Override.xml");
    } //main

    public void SerializeObject(String fileName)
    {
        // To write the file, a TextWriter is required.
        TextWriter writer = new StreamWriter(fileName);
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute that overrides the Instrument
 type.
        XmlElementAttribute attr =
            new XmlElementAttribute(Brass.class.ToType());
        attr.set_ElementName("Brass");

        // Adds the element to the collection of elements.
        attrs.get_XmlElements().Add(attr);
        attrOverrides.Add(Orchestra.class.ToType(), "instruments",
 attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(),
 attrOverrides);

        // Creates the object to serialize.
        Orchestra band = new Orchestra();

        // Creates an object of the derived type.
        Brass i = new Brass();
        i.name = "Trumpet";
        i.isValved = true;
        Instrument myInstruments[] = { i };
        band.instruments = myInstruments;
        s.Serialize(writer, band);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute that override the Instrument
 type.
        XmlElementAttribute attr =
            new XmlElementAttribute(Brass.class.ToType());
        attr.set_ElementName("Brass");

        // Adds the element to the collection of elements.
        attrs.get_XmlElements().Add(attr);
        attrOverrides.Add(Orchestra.class.ToType(), "instruments",
 attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(),
 attrOverrides);
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Orchestra band = (Orchestra)s.Deserialize(fs);

        Console.WriteLine("Brass:");

        /* Deserializing differs from serializing. To read the 
           derived-object values, declare an object of the derived 
           type (Brass) and cast the Instrument instance to it. */

        Brass b;
        for (int iCtr = 0; iCtr < band.instruments.length;
 iCtr++) {
            Instrument i = band.instruments[iCtr];
            b = (Brass)i;
            Console.WriteLine(b.name + "\n" + Convert.ToString(b.isValved));
        }
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlElementAttribute クラス
XmlElementAttribute メンバ
System.Xml.Serialization 名前空間

XmlElementAttribute コンストラクタ (Type)

XmlElementAttribute クラス新しインスタンス初期化しXmlElementAttribute適用先メンバの型を指定します。この型が使用されるのは、その型を含むオブジェクトを XmlSerializer がシリアル化または逆シリアル化する場合です。

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

Dim type As Type

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

パラメータ

type

メンバの型から派生したオブジェクトType

解説解説
使用例使用例

Instrument オブジェクト配列返すフィールド Instruments1 つ含んでいるクラス Orchestraシリアル化する例を次に示しますBrass という名前の 2 番目のクラスInstrument クラスから継承されます。この例では、XmlElementAttributeInstruments フィールド適用されBrass 型が指定されています。これにより、Instruments フィールドBrass オブジェクト受け入れるようになりますまた、ElementName プロパティ設定することによって、XML 要素の名前も指定します

Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic


Public Class Orchestra
    Public Instruments() As Instrument
End Class

Public Class Instrument
    Public Name As String
End Class

Public Class Brass
    Inherits Instrument
    Public IsValved As Boolean
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.SerializeObject("Override.xml")
        test.DeserializeObject("Override.xml")
    End Sub 'Main
    
    
    Public Sub SerializeObject(filename As
 String)
        ' To write the file, a TextWriter is required.
        Dim writer As New
 StreamWriter(filename)
        
        Dim attrOverrides As New
 XmlAttributeOverrides()
        Dim attrs As New
 XmlAttributes()
        
        ' Creates an XmlElementAttribute that overrides the Instrument
 type.
        Dim attr As New
 XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Adds the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments",
 attrs)
        
        ' Creates the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra),
 attrOverrides)
        
        ' Creates the object to serialize.
        Dim band As New
 Orchestra()
        
        ' Creates an object of the derived type.
        Dim i As New Brass()
        i.Name = "Trumpet"
        i.IsValved = True
        Dim myInstruments() As Instrument =
 {i}
        band.Instruments = myInstruments
        s.Serialize(writer, band)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(filename As
 String)
        Dim attrOverrides As New
 XmlAttributeOverrides()
        Dim attrs As New
 XmlAttributes()
        
        ' Create an XmlElementAttribute that override the Instrument
 type.
        Dim attr As New
 XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Add the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments",
 attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra),
 attrOverrides)
        
        Dim fs As New FileStream(filename,
 FileMode.Open)
        Dim band As Orchestra = CType(s.Deserialize(fs),
 Orchestra)
        Console.WriteLine("Brass:")
        
        ' Deserializing differs from serializing. To read the
        ' derived-object values, declare an object of the derived
        ' type (Brass) and cast the Instrument instance to it. 
        Dim b As Brass
        Dim i As Instrument
        For Each i In  band.Instruments
            b = CType(i, Brass)
            Console.WriteLine((b.Name + ControlChars.Cr + b.IsValved.ToString()))
        Next i
    End Sub
End Class

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)
    {
      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);
      
      XmlAttributeOverrides attrOverrides = 
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that overrides the Instrument
 type.
      XmlElementAttribute attr = new 
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

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

      // Creates the object to serialize.
      Orchestra band = new Orchestra();
      
      // Creates an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;
      s.Serialize(writer,band);
      writer.Close();
   }

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

      // Creates an XmlElementAttribute that override the Instrument
 type.
      XmlElementAttribute attr = new 
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates 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:");

      /* Deserializing differs from serializing. To read the 
         derived-object values, 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);
      }
   }
}

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Instrument
{
public:
   String^ Name;
};

public ref class Brass: public
 Instrument
{
public:
   bool IsValved;
};

public ref class Orchestra
{
public:
   array<Instrument^>^Instruments;
};

void SerializeObject( String^ filename )
{
   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that overrides the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );

   // Creates the object to serialize.
   Orchestra^ band = gcnew Orchestra;

   // Creates an object of the derived type.
   Brass^ i = gcnew Brass;
   i->Name = "Trumpet";
   i->IsValved = true;
   array<Instrument^>^myInstruments = {i};
   band->Instruments = myInstruments;
   s->Serialize( writer, band );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that override the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs ));
   Console::WriteLine( "Brass:" );

   /* Deserializing differs from serializing. To read the 
      derived-object values, declare an object of the derived 
      type (Brass) and cast the Instrument instance to it. */
   Brass^ b;
   System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Instrument^ i = safe_cast<Instrument^>(myEnum->Current);
      b = dynamic_cast<Brass^>(i);
      Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved );
   }
}

int main()
{
   SerializeObject( "Override.xml" );
   DeserializeObject( "Override.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;

public class Orchestra
{
    public Instrument instruments[];
} //Orchestra

public class Instrument
{
    public String name;
} //Instrument

public class Brass extends Instrument
{
    public boolean isValved;
} //Brass

public class Run
{
    public static void main(String[]
 args)
    {
        Run test = new Run();
        test.SerializeObject("Override.xml");
        test.DeserializeObject("Override.xml");
    } //main

    public void SerializeObject(String fileName)
    {
        // To write the file, a TextWriter is required.
        TextWriter writer = new StreamWriter(fileName);
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute that overrides the Instrument
 type.
        XmlElementAttribute attr =
            new XmlElementAttribute(Brass.class.ToType());
        attr.set_ElementName("Brass");

        // Adds the element to the collection of elements.
        attrs.get_XmlElements().Add(attr);
        attrOverrides.Add(Orchestra.class.ToType(), "instruments",
 attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(),
 attrOverrides);

        // Creates the object to serialize.
        Orchestra band = new Orchestra();

        // Creates an object of the derived type.
        Brass i = new Brass();
        i.name = "Trumpet";
        i.isValved = true;
        Instrument myInstruments[] = { i };
        band.instruments = myInstruments;
        s.Serialize(writer, band);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute that override the Instrument
 type.
        XmlElementAttribute attr =
            new XmlElementAttribute(Brass.class.ToType());
        attr.set_ElementName("Brass");

        // Adds the element to the collection of elements.
        attrs.get_XmlElements().Add(attr);
        attrOverrides.Add(Orchestra.class.ToType(), "instruments",
 attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(),
 attrOverrides);
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Orchestra band = (Orchestra)s.Deserialize(fs);

        Console.WriteLine("Brass:");

        /* Deserializing differs from serializing. To read the 
           derived-object values, declare an object of the derived 
           type (Brass) and cast the Instrument instance to it. */

        Brass b;
        for (int iCtr = 0; iCtr < band.instruments.length;
 iCtr++) {
            Instrument i = band.instruments[iCtr];
            b = (Brass)i;
            Console.WriteLine(b.name + "\n" + Convert.ToString(b.isValved));
        }
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlElementAttribute クラス
XmlElementAttribute メンバ
System.Xml.Serialization 名前空間

XmlElementAttribute コンストラクタ (String)

XML 要素の名前を指定して、XmlElementAttribute クラス新しインスタンス初期化します。

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

Public Sub New ( _
    elementName As String _
)
Dim elementName As String

Dim instance As New XmlElementAttribute(elementName)
public XmlElementAttribute (
    string elementName
)
public:
XmlElementAttribute (
    String^ elementName
)
public XmlElementAttribute (
    String elementName
)
public function XmlElementAttribute (
    elementName : String
)

パラメータ

elementName

シリアル化されたメンバXML 要素名。

解説解説

既定では、XmlSerializer でクラスインスタンスシリアル化するときは、メンバ名が XML 要素名として使用されます。たとえば、Vehicle という名前のフィールドは、Vehicle という名前の XML 要素生成します。ただし、Cars などの別の名前の要素必要な場合には、その名前を elementName パラメータ渡します

使用例使用例

Vehicles という名前のフィールド1 つある単純なクラスの例を次に示します。この例では、XmlElementAttributeフィールド適用しelementName パラメータ指定してます。これにより、XmlSerializer に対して、"Vehicles" ではなく "Cars" という名前で XML 要素生成するように指示できます

public class Transportation
{
    /** @attribute XmlElement("Cars")
     */
    public String vehicles;
} //Transportation
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlElementAttribute クラス
XmlElementAttribute メンバ
System.Xml.Serialization 名前空間

XmlElementAttribute コンストラクタ

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

名前 説明
XmlElementAttribute () XmlElementAttribute クラス新しインスタンス初期化します。

.NET Compact Framework によってサポートされています。

XmlElementAttribute (String) XML 要素の名前を指定してXmlElementAttribute クラス新しインスタンス初期化します。

.NET Compact Framework によってサポートされています。

XmlElementAttribute (Type) XmlElementAttribute クラス新しインスタンス初期化しXmlElementAttribute適用先メンバの型を指定します。この型が使用されるのは、その型を含むオブジェクトを XmlSerializer がシリアル化または逆シリアル化する場合です。

.NET Compact Framework によってサポートされています。

XmlElementAttribute (String, Type) XmlElementAttribute新しインスタンス初期化しXmlElementAttribute適用先であるメンバXML 要素の名前と派生型指定します。このメンバ型が使用されるのは、その型を含むオブジェクトXmlSerializerシリアル化する場合です。

.NET Compact Framework によってサポートされています。

参照参照

関連項目

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

XmlElementAttribute コンストラクタ ()

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

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

Dim instance As New XmlElementAttribute
public XmlElementAttribute ()
public:
XmlElementAttribute ()
public XmlElementAttribute ()
public function XmlElementAttribute ()
使用例使用例

XmlElementAttributeクラス適用する例を次に示します

Public Class MyClass1
    <XmlElement()> Public TeacherName As
 String
End Class

public class MyClass
{
   [XmlElement()]
   public string TeacherName;
}

public ref class MyClass
{
public:

   [XmlElement]
   String^ TeacherName;
};

public class MyClass
{
    /** @attribute XmlElement()
     */
    public String teacherName;
} //MyClass
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlElementAttribute クラス
XmlElementAttribute メンバ
System.Xml.Serialization 名前空間



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

辞書ショートカット

すべての辞書の索引

「XmlElementAttribute コンストラクタ」の関連用語

XmlElementAttribute コンストラクタのお隣キーワード
検索ランキング

   

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



XmlElementAttribute コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS