다음을 통해 공유


XmlElementAttribute.Type 속성

정의

XML 요소를 나타내는 데 사용되는 개체 형식을 가져오거나 설정합니다.

public:
 property Type ^ Type { Type ^ get(); void set(Type ^ value); };
public Type Type { get; set; }
public Type? Type { get; set; }
member this.Type : Type with get, set
Public Property Type As Type

속성 값

Type

멤버의 Type입니다.

예제

다음 예제에서는 속성을 사용하여 Type 에 대한 파생 개체를 지정합니다 XmlElementAttribute. 또한 이 예제에서는 3개의 인스턴스를 XmlElementAttribute 반환 ArrayList하는 필드에 적용합니다. 각 인스턴스는 필드에 허용되는 형식을 지정합니다.

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

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

public ref class Manager: public Employee
{
public:
   int Level;
};

public ref class Group
{
public:

   [XmlElement(Manager::typeid)]
   array<Employee^>^Staff;

   [XmlElement(Int32::typeid),
   XmlElement(String::typeid),
   XmlElement(DateTime::typeid)]
   ArrayList^ ExtraInfo;
};

void SerializeObject( String^ filename )
{
   // Create an XmlSerializer instance.
   XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid );

   // Create object and serialize it.
   Group^ myGroup = gcnew Group;
   Manager^ e1 = gcnew Manager;
   e1->Name = "Manager1";
   Manager^ m1 = gcnew Manager;
   m1->Name = "Manager2";
   m1->Level = 4;
   array<Employee^>^emps = {e1,m1};
   myGroup->Staff = emps;
   myGroup->ExtraInfo = gcnew ArrayList;
   myGroup->ExtraInfo->Add( ".NET" );
   myGroup->ExtraInfo->Add( 42 );
   myGroup->ExtraInfo->Add( DateTime(2001,1,1) );
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myGroup );
   writer->Close();
}

int main()
{
   SerializeObject( "TypeEx.xml" );
}
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Group
{
   [XmlElement(typeof(Manager))]
   public Employee [] Staff;

   [XmlElement (typeof(int)),
   XmlElement (typeof(string)),
   XmlElement (typeof(DateTime))]
   public ArrayList ExtraInfo;
}

public class Employee
{
   public string Name;
}

public class Manager:Employee
{
   public int Level;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("TypeEx.xml");
   }

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

      // Create object and serialize it.
      Group myGroup = new Group();

      Manager e1 = new Manager();
      e1.Name = "Manager1";
      Manager m1 =  new Manager();
      m1.Name = "Manager2";
      m1.Level = 4;

      Employee[] emps = {e1, m1};
      myGroup.Staff = emps;

      myGroup.ExtraInfo = new ArrayList();
      myGroup.ExtraInfo.Add(".NET");
      myGroup.ExtraInfo.Add(42);
      myGroup.ExtraInfo.Add(new DateTime(2001,1,1));

      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myGroup);
      writer.Close();
   }
}
Imports System.Collections
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


Public Class Group
    <XmlElement(GetType(Manager))> _    
    Public Staff() As Employee
    
    <XmlElement(GetType(Integer)), _
     XmlElement(GetType(String)), _
     XmlElement(GetType(DateTime))> _
    Public ExtraInfo As ArrayList
End Class

Public Class Employee
    Public Name As String
End Class

Public Class Manager
    Inherits Employee
    Public Level As Integer
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("TypeEx.xml")
    End Sub    
    
    Public Sub SerializeObject(filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As New XmlSerializer(GetType(Group))
        
        ' Create an object and serialize it.
        Dim myGroup As New Group()
        
        Dim e1 As New Manager()
        e1.Name = "Manager1"
        Dim m1 As New Manager()
        m1.Name = "Manager2"
        m1.Level = 4
        
        Dim emps() As Employee = {e1, m1}
        myGroup.Staff = emps
        
        myGroup.ExtraInfo = New ArrayList()
        myGroup.ExtraInfo.Add(".NET")
        myGroup.ExtraInfo.Add(42)
        myGroup.ExtraInfo.Add(New DateTime(2001, 1, 1))
        
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class

설명

필드 또는 속성에 Type 대한 파생 형식을 지정하려면 이 속성을 사용합니다.

필드 또는 속성이 반환되는 ArrayList경우 멤버의 여러 인스턴스를 XmlElementAttribute 적용할 수 있습니다. 각 인스턴스에 Type 대해 속성을 배열에 삽입할 수 있는 개체 형식으로 설정합니다.

적용 대상