XmlAttributes.XmlElements プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlAttributes.XmlElements プロパティの意味・解説 

XmlAttributes.XmlElements プロパティ

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

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

Public ReadOnly Property
 XmlElements As XmlElementAttributes
Dim instance As XmlAttributes
Dim value As XmlElementAttributes

value = instance.XmlElements
public XmlElementAttributes XmlElements { get;
 }
public:
property XmlElementAttributes^ XmlElements {
    XmlElementAttributes^ get ();
}
/** @property */
public XmlElementAttributes get_XmlElements ()
public function get XmlElements
 () : XmlElementAttributes

プロパティ
XmlElementAttribute オブジェクトコレクション格納している XmlElementAttributes。

解説解説

XML 要素としてシリアル化されたオーバーライドされる各メンバについて、Add メソッド呼び出して新しXmlElementAttributeXmlElementAttributes追加する必要があります既定では、XmlElementAttributes オブジェクト作成され XmlElements プロパティ代入されます。

使用例使用例

Transportation クラスシリアル化する例を次に示します。この例には、ArrayList を返す Vehicles という名前の単一フィールド含まれています。この例では、2 つXmlElementAttribute 属性Vehicles フィールド適用します。この例では、2 つXmlElementAttribute オブジェクト作成して、XmlAttributes オブジェクトXmlElementAttributes コレクション追加します異なったオブジェクト型配列指定できるようにするには、XmlAttributes オブジェクトを XmlAttributeOverrides オブジェクト追加します

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


Public Class Transportation
    ' Subsequent code overrides these two XmlElementAttributes.
    <XmlElement(GetType(Car)), _
     XmlElement(GetType(Plane))> _
    Public Vehicles As ArrayList
End Class

Public Class Car
    Public Name As String
End Class

Public Class Plane
    Public Name As String
End Class

Public Class Truck
    Public Name As String
End Class

Public Class Train
    Public Name As String
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("OverrideElement.xml")
    End Sub
    
    
    ' Return an XmlSerializer used for overriding.
    Public Function CreateOverrider() As
 XmlSerializer
        ' Create the XmlAttributes and XmlAttributeOverrides objects.
        Dim attrs As New
 XmlAttributes()
        
        Dim xOver As New
 XmlAttributeOverrides()
        
        
        ' Create an XmlElementAttribute to override
        ' the Vehicles property. 
        Dim xElement1 As New
 XmlElementAttribute(GetType(Truck))
        ' Add the XmlElementAttribute to the collection.
        attrs.XmlElements.Add(xElement1)
        
        ' Create a second XmlElementAttribute, and
        ' add it to the collection. 
        Dim xElement2 As New
 XmlElementAttribute(GetType(Train))
        attrs.XmlElements.Add(xElement2)
        
        ' Add the XmlAttributes to the XmlAttributeOverrides,
        ' specifying the member to override. 
        xOver.Add(GetType(Transportation), "Vehicles",
 attrs)
        
        ' Create the XmlSerializer, and return it.
        Dim xSer As New
 XmlSerializer(GetType(Transportation), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializeObject(ByVal
 filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object and serialize it.
        Dim myTransportation As New
 Transportation()
        
        ' Create two new override objects that can be
'
        ' inserted into the array. 
        myTransportation.Vehicles = New ArrayList()
        Dim myTruck As New
 Truck()
        myTruck.Name = "MyTruck"
        
        Dim myTrain As New
 Train()
        myTrain.Name = "MyTrain"
        
        myTransportation.Vehicles.Add(myTruck)
        myTransportation.Vehicles.Add(myTrain)
        
        Dim writer As New
 StreamWriter(filename)
        xSer.Serialize(writer, myTransportation)
    End Sub
End Class

using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;

public class Transportation
{
   // Subsequent code overrides these two XmlElementAttributes.
   [XmlElement(typeof(Car)),
   XmlElement(typeof(Plane))]
   public ArrayList Vehicles;
}

public class Car
{
   public string Name;
}

public class Plane
{
   public string Name;
}
public class Truck
{
   public string Name;
}
public class Train
{
   public string Name;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("OverrideElement.xml");
   }

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributes and XmlAttributeOverrides objects.
      XmlAttributes attrs = new XmlAttributes();

      XmlAttributeOverrides xOver = 
      new XmlAttributeOverrides();
      
      
      /* Create an XmlElementAttribute to override 
      the Vehicles property. */
      XmlElementAttribute xElement1 = 
      new XmlElementAttribute(typeof(Truck));
      // Add the XmlElementAttribute to the collection.
      attrs.XmlElements.Add(xElement1);

      /* Create a second XmlElementAttribute, and 
      add it to the collection. */
      XmlElementAttribute xElement2 = 
      new XmlElementAttribute(typeof(Train));
      attrs.XmlElements.Add(xElement2);

      /* Add the XmlAttributes to the XmlAttributeOverrides,
      specifying the member to override. */
      xOver.Add(typeof(Transportation), "Vehicles", attrs);
      
      // Create the XmlSerializer, and return it.
      XmlSerializer xSer = new XmlSerializer
      (typeof(Transportation), xOver);
      return xSer;
   }

   public void SerializeObject(string
 filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer xSer = CreateOverrider();

      // Create the object and serialize it.
      Transportation myTransportation = 
      new Transportation();

      /* Create two new override objects that can be
      inserted into the array. */
      myTransportation.Vehicles = new ArrayList();
      Truck myTruck = new Truck();
      myTruck.Name = "MyTruck";

      Train myTrain = new Train();
      myTrain.Name = "MyTrain";

      myTransportation.Vehicles.Add(myTruck);
      myTransportation.Vehicles.Add(myTrain);

      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myTransportation);
      
   }
}

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

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

public ref class Car
{
public:
   String^ Name;
};

public ref class Plane
{
public:
   String^ Name;
};

public ref class Truck
{
public:
   String^ Name;
};

public ref class Train
{
public:
   String^ Name;
};

public ref class Transportation
{
public:

   // Subsequent code overrides these two XmlElementAttributes.

   [XmlElement(Car::typeid),
   XmlElement(Plane::typeid)]
   ArrayList^ Vehicles;
};

// Return an XmlSerializer used for overriding.
XmlSerializer^ CreateOverrider()
{
   // Create the XmlAttributes and XmlAttributeOverrides objects.
   XmlAttributes^ attrs = gcnew XmlAttributes;
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;

   /* Create an XmlElementAttribute to override 
      the Vehicles property. */
   XmlElementAttribute^ xElement1 = gcnew XmlElementAttribute( Truck::typeid );

   // Add the XmlElementAttribute to the collection.
   attrs->XmlElements->Add( xElement1 );

   /* Create a second XmlElementAttribute, and 
      add it to the collection. */
   XmlElementAttribute^ xElement2 = gcnew XmlElementAttribute( Train::typeid );
   attrs->XmlElements->Add( xElement2 );

   /* Add the XmlAttributes to the XmlAttributeOverrides,
      specifying the member to override. */
   xOver->Add( Transportation::typeid, "Vehicles", attrs );

   // Create the XmlSerializer, and return it.
   XmlSerializer^ xSer = gcnew XmlSerializer( Transportation::typeid,xOver );
   return xSer;
}

void SerializeObject( String^ filename )
{
   // Create an XmlSerializer instance.
   XmlSerializer^ xSer = CreateOverrider();

   // Create the object and serialize it.
   Transportation^ myTransportation = gcnew Transportation;

   /* Create two new override objects that can be
      inserted into the array. */
   myTransportation->Vehicles = gcnew ArrayList;
   Truck^ myTruck = gcnew Truck;
   myTruck->Name = "MyTruck";
   Train^ myTrain = gcnew Train;
   myTrain->Name = "MyTrain";
   myTransportation->Vehicles->Add( myTruck );
   myTransportation->Vehicles->Add( myTrain );
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myTransportation );
}

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

public class Transportation
{
    // Subsequent code overrides these two XmlElementAttributes.
    /** @attribute XmlElement(Car.class)
        @attribute XmlElement(Plane.class)
     */
    public ArrayList vehicles;
} //Transportation

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

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

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

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

public class Test
{
    public static void main(String[]
 args)
    {
        Test t = new Test();
        t.SerializeObject("OverrideElement.xml");
    } //main

    // Return an XmlSerializer used for overriding.
    public XmlSerializer CreateOverrider()
    {
        // Create the XmlAttributes and XmlAttributeOverrides objects.
        XmlAttributes attrs = new XmlAttributes();
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();

        /* Create an XmlElementAttribute to override 
           the Vehicles property. */
        XmlElementAttribute xElement1 =
            new XmlElementAttribute(Truck.class.ToType());

        // Add the XmlElementAttribute to the collection.
        attrs.get_XmlElements().Add(xElement1);

        /* Create a second XmlElementAttribute, and 
           add it to the collection. */
        XmlElementAttribute xElement2 =
            new XmlElementAttribute(Train.class.ToType());
        attrs.get_XmlElements().Add(xElement2);

        /* Add the XmlAttributes to the XmlAttributeOverrides,
           specifying the member to override. */
        xOver.Add(Transportation.class.ToType(), "vehicles",
 attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer =
            new XmlSerializer(Transportation.class.ToType(),
 xOver);
        return xSer;
    } //CreateOverrider

    public void SerializeObject(String fileName)
    {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Transportation myTransportation = new Transportation();

        /* Create two new override objects that can be
           inserted into the array. */
        myTransportation.vehicles = new ArrayList();
        Truck myTruck = new Truck();
        myTruck.name = "MyTruck";

        Train myTrain = new Train();
        myTrain.name = "MyTrain";

        myTransportation.vehicles.Add(myTruck);
        myTransportation.vehicles.Add(myTrain);
        TextWriter writer = new StreamWriter(fileName);
        xSer.Serialize(writer, myTransportation);
    } //SerializeObject
} //Test
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlAttributes クラス
XmlAttributes メンバ
System.Xml.Serialization 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からXmlAttributes.XmlElements プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からXmlAttributes.XmlElements プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からXmlAttributes.XmlElements プロパティ を検索

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

辞書ショートカット

すべての辞書の索引

「XmlAttributes.XmlElements プロパティ」の関連用語

XmlAttributes.XmlElements プロパティのお隣キーワード
検索ランキング

   

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



XmlAttributes.XmlElements プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS