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

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

XmlArrayItemAttribute コンストラクタ ()

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

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

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

Vehicle オブジェクト配列返す MyVehicles というフィールドがある Transportation というクラスシリアル化する例を次に示します。この例では、XmlArrayItemAttributeフィールド適用しVehicle クラスから派生した Car クラスインスタンスを XmlSerializer が配列挿入できるようにします。

Option Explicit
Option Strict

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


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArrayItem(), _
     XmlArrayItem(GetType(Car), ElementName := "Automobile")>
 _
    Public MyVehicles() As Vehicle
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.SerializeObject("XmlArrayItem1.xml")
        test.DeserializeObject("XmlArrayItem1.xml")
    End Sub
    
    
    Private Sub SerializeObject(ByVal
 filename As String)
        ' Creates an XmlSerializer for the Transportation class. 
        Dim MySerializer As New
 XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New
 StreamWriter(filename)
        
        ' Creates the object to serialize.
        Dim myTransportation As New
 Transportation()
        
        ' Creates objects to add to the array.
        Dim myVehicle As New
 Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New
 Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        myTransportation.MyVehicles = New Vehicle() {myVehicle,
 myCar}
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub
    
    
    Private Sub DeserializeObject(ByVal
 filename As String)
        ' Create an XmlSerializer instance.
        Dim mySerializer As New
 XmlSerializer(GetType(Transportation))
        Dim myFileStream As New
 FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation
 = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length
 - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;   
}
 
public class Transportation
{  
   [XmlArrayItem(), 
   XmlArrayItem(typeof(Car), ElementName = "Automobile")]
   public Vehicle[] MyVehicles;
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("XmlArrayItem1.xml");
      test.DeserializeObject("XmlArrayItem1.xml");
   }

   private void SerializeObject(string
 filename){
      // Creates an XmlSerializer for the Transportation class. 
      XmlSerializer MySerializer = new XmlSerializer(typeof(Transportation));
   
      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);

      // Creates the object to serialize.
      Transportation myTransportation = new Transportation();

      // Creates objects to add to the array.
      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";

      myTransportation.MyVehicles = 
      new Vehicle[2] {myVehicle, myCar};
      
      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }
 
   private void DeserializeObject(string
 filename)
   {
      // Creates an XmlSerializer instance.
      XmlSerializer mySerializer = new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);
 
      for(int i = 0; i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
 }
#using <System.Xml.dll>
#using <System.dll>

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

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public
 Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArrayItem,
   XmlArrayItem(Car::typeid,ElementName="Automobile")]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer for the Transportation class. 
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );

   // Creates the object to serialize.
   Transportation^ myTransportation = gcnew Transportation;

   // Creates objects to add to the array.
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^temp = {myVehicle,myCar};
   myTransportation->MyVehicles = temp;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates an XmlSerializer instance.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize(
 myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length;
 i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

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

public class Vehicle
{
    public String id;
} //Vehicle

public class Car extends Vehicle
{
    public String maker;
} //Car

public class Transportation
{
    /** @attribute XmlArrayItem()
        @attribute XmlArrayItem(Car.class, ElementName = "Automobile")
     */
    public Vehicle myVehicles[];
} //Transportation

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

    private void SerializeObject(String fileName)
    {
        // Creates an XmlSerializer for the Transportation class. 
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());

        // Writing the XML file to disk requires a TextWriter.
        TextWriter myTextWriter = new StreamWriter(fileName);
        
        // Creates the object to serialize.
        Transportation myTransportation = new Transportation();

        // Creates objects to add to the array.
        Vehicle myVehicle = new Vehicle();
        myVehicle.id = "A12345";

        Car myCar = new Car();
        myCar.id = "Car 34";
        myCar.maker = "FamousCarMaker";

        myTransportation.myVehicles = new Vehicle[] { myVehicle,
 myCar };

        // Serializes the object, and closes the StreamWriter.
        mySerializer.Serialize(myTextWriter, myTransportation);
        myTextWriter.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        // Creates an XmlSerializer instance.
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());
        FileStream myFileStream = new FileStream(fileName, FileMode.Open);
        Transportation myTransportation =
            (Transportation)mySerializer.Deserialize(myFileStream);

        for (int i = 0; i < myTransportation.myVehicles.length;
 i++) {
            Console.WriteLine(myTransportation.myVehicles[i].id);
        }
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlArrayItemAttribute クラス
XmlArrayItemAttribute メンバ
System.Xml.Serialization 名前空間

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

XmlArrayItemAttribute クラス新しインスタンス初期化しXML ドキュメント生成されXML 要素の名前、および生成されXML ドキュメント挿入できる Type指定します

名前空間: 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 XmlArrayItemAttribute(elementName,
 type)
public XmlArrayItemAttribute (
    string elementName,
    Type type
)
public:
XmlArrayItemAttribute (
    String^ elementName, 
    Type^ type
)
public XmlArrayItemAttribute (
    String elementName, 
    Type type
)
public function XmlArrayItemAttribute (
    elementName : String, 
    type : Type
)

パラメータ

elementName

XML 要素の名前。

type

シリアル化するオブジェクトType

解説解説
使用例使用例

Vehicle オブジェクト配列返す MyVehicles というフィールドがある Transportation というクラスシリアル化する例を次に示します。この例では、XmlArrayItemAttributeフィールド適用しVehicle クラスから派生した Car クラスインスタンスを XmlSerializer が配列挿入できるようにします。属性適用するときに、この例では elementName パラメータ使用して ElementName プロパティ設定しtype パラメータ使用して Type プロパティ設定してます。

Option Explicit
Option Strict

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


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArray(), _
     XmlArrayItem("Transport", GetType(Vehicle)),
 _
     XmlArrayItem("Automobile", GetType(Car))>
 _
    Public MyVehicles() As Vehicle
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.SerializeObject("XmlArrayItem4.xml")
        test.DeserializeObject("XmlArrayItem4.xml")
    End Sub    
    
    Private Sub SerializeObject(ByVal
 filename As String)
        ' Creates an XmlSerializer for the Transportation class.
        Dim MySerializer As New
 XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New
 StreamWriter(filename)
        
        Dim myTransportation As New
 Transportation()
        
        Dim myVehicle As New
 Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New
 Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        Dim myVehicles() As Vehicle =  {myVehicle,
 myCar}
        myTransportation.MyVehicles = myVehicles
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub    
    
    Private Sub DeserializeObject(ByVal
 filename As String)
        ' Creates an XmlSerializer.
        Dim mySerializer As New
 XmlSerializer(GetType(Transportation))
        Dim myFileStream As New
 FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation
 = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length
 - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;
}
 
public class Transportation
{  
   [XmlArray]
   [XmlArrayItem("Transport", typeof(Vehicle)), 
   XmlArrayItem("Automobile", typeof(Car))]
   public Vehicle[] MyVehicles;
}
 
public class Run
{
   public static void Main()
   {
      Run test= new Run();
      test.SerializeObject("XmlArrayItem4.xml");
      test.DeserializeObject("XmlArrayItem4.xml");
   }
 
   private void SerializeObject(string
 filename)
   {
       // Creates an XmlSerializer for the Transportation class.
      XmlSerializer MySerializer = 
      new XmlSerializer(typeof(Transportation));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);
 
      Transportation myTransportation = new Transportation();

      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";
       
      Vehicle [] myVehicles = {myVehicle, myCar};
      myTransportation.MyVehicles = myVehicles;

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }
 
   private void DeserializeObject(string
 filename)
   {
      // Creates an XmlSerializer.
      XmlSerializer mySerializer = 
      new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);
 
      for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
}
   
#using <System.Xml.dll>
#using <System.dll>

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

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public
 Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArray]
   [XmlArrayItem("Transport",Vehicle::typeid),
   XmlArrayItem("Automobile",Car::typeid)]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer for the Transportation class.
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );
   Transportation^ myTransportation = gcnew Transportation;
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^myVehicles = {myVehicle,myCar};
   myTransportation->MyVehicles = myVehicles;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates an XmlSerializer.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize(
 myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length;
 i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

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

public class Vehicle
{
    public String id;
} //Vehicle

public class Car extends Vehicle
{
    public String maker;
} //Car

public class Transportation
{
    /** @attribute XmlArray()
     */
    /** @attribute XmlArrayItem("Transport", Vehicle.class)
        @attribute XmlArrayItem("Automobile", Car.class)
     */
    public Vehicle myVehicles[];
} //Transportation

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

    private void SerializeObject(String fileName)
    {
        // Creates an XmlSerializer for the Transportation class.
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());
        
        // Writing the XML file to disk requires a TextWriter.
        TextWriter myTextWriter = new StreamWriter(fileName);
        Transportation myTransportation = new Transportation();
        
        Vehicle myVehicle = new Vehicle();
        myVehicle.id = "A12345";

        Car myCar = new Car();
        myCar.id = "Car 34";
        myCar.maker = "FamousCarMaker";

        Vehicle myVehiclesObj[] =  { myVehicle, myCar };
        myTransportation.myVehicles = myVehiclesObj;

        // Serializes the object, and closes the StreamWriter.
        mySerializer.Serialize(myTextWriter, myTransportation);
        myTextWriter.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        // Creates an XmlSerializer.
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());
        FileStream myFileStream = new FileStream(fileName, FileMode.Open);
        Transportation myTransportation =
            (Transportation)mySerializer.Deserialize(myFileStream);

        for (int i = 0; i < myTransportation.myVehicles.length;
 i++) {
            Console.WriteLine(myTransportation.myVehicles[i].id);
        }
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlArrayItemAttribute クラス
XmlArrayItemAttribute メンバ
System.Xml.Serialization 名前空間

XmlArrayItemAttribute コンストラクタ (Type)

XmlArrayItemAttribute クラス新しインスタンス初期化しシリアル化された配列挿入できる Type指定します

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

Dim type As Type

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

パラメータ

type

シリアル化するオブジェクトType

使用例使用例

Vehicle オブジェクト配列返す MyVehicles というフィールドがある Transportation というクラスシリアル化する例を次に示します。この例では、XmlArrayItemAttributeフィールド適用しVehicle クラスから派生した Car クラスインスタンスを XmlSerializer が配列挿入できるようにします。属性適用するときに、この例では type パラメータ使用して Type プロパティ設定してます。

Option Explicit
Option Strict

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


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArrayItem(GetType(Vehicle)), _
     XmlArrayItem(GetType(Car))> _
    Public MyVehicles() As Vehicle
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.SerializeObject("XmlArrayItem3.xml")
        test.DeserializeObject("XmlArrayItem3.xml")
    End Sub    
    
    Private Sub SerializeObject(ByVal
 filename As String)
        ' Creates an XmlSerializer object. 
        Dim MySerializer As New
 XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New
 StreamWriter(filename)
        
        Dim myTransportation As New
 Transportation()
        
        Dim myVehicle As New
 Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New
 Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        Dim myVehicles() As Vehicle =  {myVehicle,
 myCar}
        myTransportation.MyVehicles = myVehicles
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub    
    
    Private Sub DeserializeObject(ByVal
 filename As String)
        ' Creates the serializer with the type to deserialize.
        Dim mySerializer As New
 XmlSerializer(GetType(Transportation))
        Dim myFileStream As New
 FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation
 = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length
 - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;
}
 
public class Transportation
{  
   [XmlArrayItem(typeof(Vehicle)), 
   XmlArrayItem(typeof(Car))]
   public Vehicle[] MyVehicles;
}
 
public class Run
{
   public static void Main()
   {
      Run test= new Run();
      test.SerializeObject("XmlArrayItem3.xml");
      test.DeserializeObject("XmlArrayItem3.xml");
   }
 
   private void SerializeObject(string
 filename)
   {
       // Creates an XmlSerializer. 
      XmlSerializer MySerializer = 
      new XmlSerializer(typeof(Transportation));
      
      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);
 
      Transportation myTransportation = new Transportation();

      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";
       
      Vehicle [] myVehicles = {myVehicle, myCar};
      myTransportation.MyVehicles = myVehicles;

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }
 
   private void DeserializeObject(string
 filename)
   {
      // Creates the serializer with the type to deserialize.
      XmlSerializer mySerializer = 
      new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);
 
      for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
}
   
#using <System.Xml.dll>
#using <System.dll>

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

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public
 Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArrayItem(Vehicle::typeid),
   XmlArrayItem(Car::typeid)]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer. 
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );
   Transportation^ myTransportation = gcnew Transportation;
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^myVehicles = {myVehicle,myCar};
   myTransportation->MyVehicles = myVehicles;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates the serializer with the type to deserialize.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize(
 myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length;
 i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

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

public class Vehicle
{
    public String id;
} //Vehicle

public class Car extends Vehicle
{
    public String maker;
} //Car

public class Transportation
{
    /** @attribute XmlArrayItem(Vehicle.class)
        @attribute XmlArrayItem(Car.class)
     */
    public Vehicle myVehicles[];
} //Transportation

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

    private void SerializeObject(String fileName)
    {
        // Creates an XmlSerializer. 
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());
        
        // Writing the XML file to disk requires a TextWriter.
        TextWriter myTextWriter = new StreamWriter(fileName);
        Transportation myTransportation = new Transportation();
        
        Vehicle myVehicle = new Vehicle();
        myVehicle.id = "A12345";

        Car myCar = new Car();
        myCar.id = "Car 34";
        myCar.maker = "FamousCarMaker";

        Vehicle myVehiclesObj[] =  { myVehicle, myCar };
        myTransportation.myVehicles = myVehiclesObj;

        // Serializes the object, and closes the StreamWriter.
        mySerializer.Serialize(myTextWriter, myTransportation);
        myTextWriter.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        // Creates the serializer with the type to deserialize.
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());
        FileStream myFileStream =
            new FileStream(fileName, FileMode.Open);
        Transportation myTransportation =
            (Transportation)mySerializer.Deserialize(myFileStream);

        for (int i = 0; i < myTransportation.myVehicles.length;
 i++) {
            Console.WriteLine(myTransportation.myVehicles[i].id);
        }
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlArrayItemAttribute クラス
XmlArrayItemAttribute メンバ
System.Xml.Serialization 名前空間

XmlArrayItemAttribute コンストラクタ

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

参照参照

関連項目

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

XmlArrayItemAttribute コンストラクタ (String)

XmlArrayItemAttribute クラス新しインスタンス初期化しXML ドキュメント生成される XML 要素の名前を指定します

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

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

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

パラメータ

elementName

XML 要素の名前。

解説解説
使用例使用例

Vehicle オブジェクト配列返す MyVehicles というフィールドがある Transportation というクラスシリアル化する例を次に示します。この例では、XmlArrayItemAttributeフィールド適用しVehicle クラスから派生した Car クラスインスタンスを XmlSerializer が配列挿入できるようにします。属性適用するときに、この例では elementName パラメータ使用して ElementName プロパティ設定してます。

Option Explicit
Option Strict

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


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArrayItem(ElementName := "Transportation"),
 _
     XmlArrayItem(GetType(Car), ElementName := "Automobile")>
 _
    Public MyVehicles() As Vehicle
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.SerializeObject("XmlArrayItem2.xml")
        test.DeserializeObject("XmlArrayItem2.xml")
    End Sub    
    
    Private Sub SerializeObject(ByVal
 filename As String)
        ' Creates an XmlSerializer for the Transportation class.
        Dim MySerializer As New
 XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New
 StreamWriter(filename)
        
        Dim myTransportation As New
 Transportation()
        
        Dim myVehicle As New
 Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New
 Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        Dim myVehicles() As Vehicle = {myVehicle,
 myCar}
        myTransportation.MyVehicles = myVehicles
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub
        
    Private Sub DeserializeObject(ByVal
 filename As String)
        ' Create the serializer with the type to deserialize.
        Dim mySerializer As New
 XmlSerializer(GetType(Transportation))
        Dim myFileStream As New
 FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation
 = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length
 - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;
}
 
public class Transportation
{  
   [XmlArrayItem(ElementName = "Transportation"), 
   XmlArrayItem(typeof(Car), ElementName = "Automobile")]
   public Vehicle[] MyVehicles;
}
 
public class Run
{
   public static void Main()
   {
      Run test= new Run();
      test.SerializeObject("XmlArrayItem2.xml");
      test.DeserializeObject("XmlArrayItem2.xml");
   }
 
   private void SerializeObject(string
 filename)
   {
      // Creates an XmlSerializer for the Transportation class.
      XmlSerializer MySerializer = 
      new XmlSerializer(typeof(Transportation));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);
 
      Transportation myTransportation = new Transportation();

      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";
       
      Vehicle [] myVehicles = {myVehicle, myCar};
      myTransportation.MyVehicles = myVehicles;

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }
 
   private void DeserializeObject(string
 filename)
   {
      // Creates the serializer with the type to deserialize.
      XmlSerializer mySerializer = 
      new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);
 
      for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
}
   
#using <System.Xml.dll>
#using <System.dll>

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

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public
 Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArrayItem(ElementName="Transportation"),
   XmlArrayItem(Car::typeid,ElementName="Automobile")]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer for the Transportation class.
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );
   Transportation^ myTransportation = gcnew Transportation;
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^myVehicles = {myVehicle,myCar};
   myTransportation->MyVehicles = myVehicles;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates the serializer with the type to deserialize.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize(
 myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length;
 i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

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

public class Vehicle
{
    public String id;
} //Vehicle

public class Car extends Vehicle
{
    public String maker;
} //Car

public class Transportation
{
    /** @attribute XmlArrayItem(ElementName = "Transportation")
        @attribute XmlArrayItem(Car.class, ElementName = "Automobile")
     */
    public Vehicle myVehicles[];
} //Transportation

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

    private void SerializeObject(String fileName)
    {
        // Creates an XmlSerializer for the Transportation class.
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());

        // Writing the XML file to disk requires a TextWriter.
        TextWriter myTextWriter = new StreamWriter(fileName);
        Transportation myTransportation = new Transportation();
        
        Vehicle myVehicle = new Vehicle();
        myVehicle.id = "A12345";

        Car myCar = new Car();
        myCar.id = "Car 34";
        myCar.maker = "FamousCarMaker";

        Vehicle myVehiclesObj[] =  { myVehicle, myCar };
        myTransportation.myVehicles = myVehiclesObj;

        // Serializes the object, and closes the StreamWriter.
        mySerializer.Serialize(myTextWriter, myTransportation);
        myTextWriter.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        // Creates the serializer with the type to deserialize.
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());
        FileStream myFileStream =
            new FileStream(fileName, FileMode.Open);
        Transportation myTransportation =
            (Transportation)mySerializer.Deserialize(myFileStream);

        for (int i = 0; i < myTransportation.myVehicles.length;
 i++) {
            Console.WriteLine(myTransportation.myVehicles[i].id);
        }
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlArrayItemAttribute クラス
XmlArrayItemAttribute メンバ
System.Xml.Serialization 名前空間



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

辞書ショートカット

すべての辞書の索引

「XmlArrayItemAttribute コンストラクタ ()」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS