次の方法で共有


XmlAttributes.XmlType プロパティ

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

Public Property XmlType As XmlTypeAttribute
[C#]
public XmlTypeAttribute XmlType {get; set;}
[C++]
public: __property XmlTypeAttribute* get_XmlType();public: __property void set_XmlType(XmlTypeAttribute*);
[JScript]
public function get XmlType() : XmlTypeAttribute;public function set XmlType(XmlTypeAttribute);

プロパティ値

クラス宣言に適用された XmlTypeAttribute をオーバーライドする XmlTypeAttribute

解説

XmlTypeAttribute を使用して、型が XmlSerializer によってどのようにシリアル化されるかを制御できます。たとえば、既定では、型がシリアル化されるときは、 XmlSerializer はクラス名を XML 要素名として使用します。 XmlTypeAttribute を作成し、それに XmlType プロパティを設定し、 XmlAttributeOverrides オブジェクトを作成することによって、XML 要素名を変更できます。

使用例

[Visual Basic, C#, C++] XmlTypeAttribute オブジェクトを作成し、そのオブジェクトを XmlAttributes オブジェクトの XmlType プロパティに代入する例を次に示します。

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


Public Class Transportation
    Public Cars() As Car
End Class

Public Class Car
    Public ID As Integer
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("XmlType.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 XmlTypeAttribute and change the name of the
        ' XML type. 
        Dim xType As New XmlTypeAttribute()
        xType.TypeName = "Autos"
        
        ' Set the XmlTypeAttribute to the XmlType property.
        attrs.XmlType = xType
        
        ' Add the XmlAttributes to the XmlAttributeOverrides,
        ' specifying the member to override. 
        xOver.Add(GetType(Car), 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 an object and serialize it.
        Dim myTransportation As New Transportation()
        
        Dim c1 As New Car()
        c1.ID = 12
        
        Dim c2 As New Car()
        c2.ID = 44
        
        myTransportation.Cars = New Car(1) {c1, c2}
        
        ' To write the file, a TextWriter is required.
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myTransportation)
    End Sub
End Class


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

public class Transportation
{
   public Car[] Cars;
}

public class Car
{
   public int ID;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("XmlType.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 XmlTypeAttribute and change the name of the 
         XML type. */
      XmlTypeAttribute xType = new XmlTypeAttribute();
      xType.TypeName = "Autos";

      // Set the XmlTypeAttribute to the XmlType property.
      attrs.XmlType = xType;

      /* Add the XmlAttributes to the XmlAttributeOverrides,
         specifying the member to override. */
      xOver.Add(typeof(Car), 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 object and serialize it.
      Transportation myTransportation = 
      new Transportation();

      Car c1 = new Car();
      c1.ID = 12;

      Car c2 = new Car();
      c2.ID = 44;

      myTransportation.Cars = new Car[2]{c1,c2};

      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myTransportation);
   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public __gc class Car
{
public:
   int ID;
};

public __gc class Transportation
{
public:
   Car* Cars[];
};

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

   /* Create an XmlTypeAttribute and change the name of the 
   XML type. */
   XmlTypeAttribute* xType = new XmlTypeAttribute();
   xType->TypeName = S"Autos";

   // Set the XmlTypeAttribute to the XmlType property.
   attrs->XmlType = xType;

   /* Add the XmlAttributes to the XmlAttributeOverrides,
   specifying the member to override. */
   xOver->Add(__typeof(Car), attrs);

   // Create the XmlSerializer, and return it.
   XmlSerializer* xSer = new XmlSerializer
      (__typeof(Transportation), xOver);
   return xSer;
}

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

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

   Car* c1 = new Car();
   c1->ID = 12;

   Car* c2 = new Car();
   c2->ID = 44;

   Car* temp0 [] = {c1,c2};
   myTransportation->Cars = temp0;

   // To write the file, a TextWriter is required.
   TextWriter* writer = new StreamWriter(filename);
   xSer->Serialize(writer, myTransportation);
}

int main()
{
   SerializeObject(S"XmlType.xml");
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

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