다음을 통해 공유


XmlAttributes.XmlType 속성

정의

XmlSerializer가 적용된 클래스를 XmlTypeAttribute가 serialize하는 방식을 지정하는 개체를 가져오거나 설정합니다.

public:
 property System::Xml::Serialization::XmlTypeAttribute ^ XmlType { System::Xml::Serialization::XmlTypeAttribute ^ get(); void set(System::Xml::Serialization::XmlTypeAttribute ^ value); };
public System.Xml.Serialization.XmlTypeAttribute XmlType { get; set; }
public System.Xml.Serialization.XmlTypeAttribute? XmlType { get; set; }
member this.XmlType : System.Xml.Serialization.XmlTypeAttribute with get, set
Public Property XmlType As XmlTypeAttribute

속성 값

XmlTypeAttribute

클래스 선언에 적용된 XmlTypeAttribute를 재정의하는 XmlTypeAttribute입니다.

예제

다음 예제에서는 XmlTypeAttribute 개체를 할당 합니다 XmlType 의 속성을 XmlAttributes 개체.

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

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Car
{
public:
   int ID;
};

public ref class Transportation
{
public:
   array<Car^>^Cars;
};

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

   /* Create an XmlTypeAttribute and change the name of the 
      XML type. */
   XmlTypeAttribute^ xType = gcnew 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( Car::typeid, 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 object and serialize it.
   Transportation^ myTransportation = gcnew Transportation;
   Car^ c1 = gcnew Car;
   c1->ID = 12;
   Car^ c2 = gcnew Car;
   c2->ID = 44;
   array<Car^>^temp0 = {c1,c2};
   myTransportation->Cars = temp0;

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

int main()
{
   SerializeObject( "XmlType.xml" );
}
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);
   }
}
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

설명

합니다 XmlTypeAttribute 형식으로 serialize 되는 방식을 제어 하는데 사용할 수는 XmlSerializer합니다. 예를 들어 형식으로 serialize 될 때 기본적으로는 XmlSerializer XML 요소 이름으로 클래스 이름을 사용 합니다. 만들어를 XmlTypeAttribute설정 합니다 XmlType 속성을 만들고을 XmlAttributeOverrides 개체를 XML 요소 이름을 변경할 수 있습니다.

적용 대상