XmlTextAttribute 클래스

정의

멤버가 포함된 클래스가 직렬화되거나 역직렬화될 때 멤버를 XML 텍스트로 처리하도록 XmlSerializer에 지정합니다.

public ref class XmlTextAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlTextAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlTextAttribute = class
    inherit Attribute
Public Class XmlTextAttribute
Inherits Attribute
상속
XmlTextAttribute
특성

예제

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

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

public ref class Group1
{
public:

   // The XmlTextAttribute with type set to string informs the 
   // XmlSerializer that strings should be serialized as XML text.

   [XmlText(String::typeid)]
   [XmlElement(Int32::typeid)]
   [XmlElement(Double::typeid)]
   array<Object^>^All;
   Group1()
   {
      array<Object^>^temp = {321,"One",2,3.0,"Two"};
      All = temp;
   }
};

public enum class GroupType
{
   Small, Medium, Large
};

public ref class Group2
{
public:

   [XmlText(Type=GroupType::typeid)]
   GroupType Type;
};

public ref class Group3
{
public:

   [XmlText(Type=DateTime::typeid)]
   DateTime CreationTime;
   Group3()
   {
      CreationTime = DateTime::Now;
   }
};

public ref class Test
{
public:
   static void main()
   {
      Test^ t = gcnew Test;
      t->SerializeArray( "XmlText1.xml" );
      t->SerializeEnum( "XmlText2.xml" );
      t->SerializeDateTime( "XmlText3.xml" );
   }

private:
   void SerializeArray( String^ filename )
   {
      XmlSerializer^ ser = gcnew XmlSerializer( Group1::typeid );
      Group1^ myGroup1 = gcnew Group1;
      TextWriter^ writer = gcnew StreamWriter( filename );
      ser->Serialize( writer, myGroup1 );
      writer->Close();
   }

   void SerializeEnum( String^ filename )
   {
      XmlSerializer^ ser = gcnew XmlSerializer( Group2::typeid );
      Group2^ myGroup = gcnew Group2;
      myGroup->Type = GroupType::Medium;
      TextWriter^ writer = gcnew StreamWriter( filename );
      ser->Serialize( writer, myGroup );
      writer->Close();
   }

   void SerializeDateTime( String^ filename )
   {
      XmlSerializer^ ser = gcnew XmlSerializer( Group3::typeid );
      Group3^ myGroup = gcnew Group3;
      TextWriter^ writer = gcnew StreamWriter( filename );
      ser->Serialize( writer, myGroup );
      writer->Close();
   }
};

int main()
{
   Test::main();
}
using System;
using System.Xml.Serialization;
using System.IO;

public class Group1{
   // The XmlTextAttribute with type set to string informs the
   // XmlSerializer that strings should be serialized as XML text.
   [XmlText(typeof(string))]
   [XmlElement(typeof(int))]
   [XmlElement(typeof(double))]
   public object [] All= new object []{321, "One", 2, 3.0, "Two" };
}

public class Group2{
   [XmlText(Type = typeof(GroupType))]
   public GroupType Type;
}
public enum GroupType{
   Small,
   Medium,
   Large
}

public class Group3{
   [XmlText(Type=typeof(DateTime))]
   public DateTime CreationTime = DateTime.Now;
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.SerializeArray("XmlText1.xml");
      t.SerializeEnum("XmlText2.xml");
      t.SerializeDateTime("XmlText3.xml");
   }

   private void SerializeArray(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group1));
      Group1 myGroup1 = new Group1();

      TextWriter writer = new StreamWriter(filename);

      ser.Serialize(writer, myGroup1);
      writer.Close();
   }

   private void SerializeEnum(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group2));
      Group2 myGroup = new Group2();
      myGroup.Type = GroupType.Medium;
      TextWriter writer = new StreamWriter(filename);

      ser.Serialize(writer, myGroup);
      writer.Close();
   }

   private void SerializeDateTime(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group3));
      Group3 myGroup = new Group3();
      TextWriter writer = new StreamWriter(filename);

      ser.Serialize(writer, myGroup);
      writer.Close();
   }
}
Imports System.Xml.Serialization
Imports System.IO


Public Class Group1
   ' The XmlTextAttribute with type set to String informs the 
   ' XmlSerializer that strings should be serialized as XML text.
   <XmlText(GetType(String)), _
   XmlElement(GetType(integer)), _  
   XmlElement(GetType(double))> _
   public All () As Object = _
   New Object (){321, "One", 2, 3.0, "Two" }
End Class


Public Class Group2
   <XmlText(GetType(GroupType))> _
   public Type As GroupType 
End Class

Public Enum GroupType
   Small
   Medium
   Large
End Enum

Public Class Group3
   <XmlText(GetType(DateTime))> _
   Public CreationTime As DateTime = DateTime.Now
End Class

Public Class Test
   Shared Sub Main()
      Dim t As Test = New Test()
      t.SerializeArray("XmlText1.xml")
      t.SerializeEnum("XmlText2.xml")
      t.SerializeDateTime("XmlText3.xml")
   End Sub

   Private Sub SerializeArray(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group1))
      Dim myGroup1 As Group1 = New Group1()

      Dim writer As TextWriter = New StreamWriter(filename)

      ser.Serialize(writer, myGroup1)
      writer.Close()
   End Sub

   Private Sub SerializeEnum(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group2))
      Dim myGroup As Group2 = New Group2()
      myGroup.Type = GroupType.Medium
      Dim writer As TextWriter = New StreamWriter(filename)

      ser.Serialize(writer, myGroup)
      writer.Close()
   End Sub

   Private Sub SerializeDateTime(filename As String)
      Dim ser As XmlSerializer = new XmlSerializer(GetType(Group3))
      Dim myGroup As Group3 = new Group3()
      Dim writer As TextWriter = new StreamWriter(filename)

      ser.Serialize(writer, myGroup)
      writer.Close()
   End Sub
End Class

설명

개체 XmlTextAttribute 와 메서드를 통해 개체를 직렬화하고 역직렬화하는 방법을 XmlSerializer 제어하는 특성 패밀리에 Serialize Deserialize 속합니다. 유사한 특성의 전체 목록은 XML Serialization을 제어하는 특성을 참조하세요.

클래스의 인스턴스 하나 XmlTextAttribute 만 클래스에 적용할 수 있습니다.

기본 형식 및 열거형 형식을 반환하는 공용 필드 및 공용 읽기/쓰기 속성에 적용 XmlTextAttribute 할 수 있습니다.

문자열 배열을 XmlTextAttribute 반환하는 필드 또는 속성에 적용할 수 있습니다. 형식 Object 배열에 특성을 적용할 수도 있지만 속성을 문자열로 Type 설정해야 합니다. 이 경우 배열에 삽입된 모든 문자열은 XML 텍스트로 직렬화됩니다.

XmlTextAttribute 개체 배열 또는 XmlNode 개체를 반환 XmlNode 하는 필드에도 적용할 수 있습니다.

기본적으로 클래스 멤버를 XmlSerializer XML 요소로 직렬화합니다. 그러나 멤버에 XmlTextAttribute 적용하면 해당 값이 XmlSerializer XML 텍스트로 변환됩니다. 즉, 값이 XML 요소의 콘텐츠로 인코딩됩니다.

XML 스키마 정의 도구(Xsd.exe)는 XSD(XML 스키마 정의) 파일에서 클래스를 만들 때 가끔 생성 XmlTextAttribute 합니다. 이는 스키마에 혼합 콘텐츠가 complexType 포함된 경우 발생합니다. 이 경우 해당 클래스에는 적용된 문자열 배열을 반환하는 멤버가 XmlTextAttribute 포함됩니다. 예를 들어 도구에서 이 스키마를 Xml Schema Definition 처리하는 경우는 다음과 같습니다.

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace=""   
xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  <xs:element name="LinkList" type="LinkList" />  
  <xs:complexType name="LinkList" mixed="true">  
    <xs:sequence>  
      <xs:element minOccurs="1" maxOccurs="1" name="id" type="xs:int" />  
      <xs:element minOccurs="0" maxOccurs="1" name="name" type="xs:string" />  
      <xs:element minOccurs="0" maxOccurs="1" name="next" type="LinkList" />  
    </xs:sequence>  
  </xs:complexType>  
</xs:schema>  

다음 클래스가 생성됩니다(추가 공백 및 설명이 제거됨).

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class LinkList
{

    private int idField;
    private string nameField;
    private LinkList nextField;
    private string[] textField;

    public int id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    public LinkList next
    {
        get
        {
            return this.nextField;
        }
        set
        {
            this.nextField = value;
        }
    }

    [System.Xml.Serialization.XmlTextAttribute()]
    public string[] Text
    {
        get
        {
            return this.textField;
        }
        set
        {
            this.textField = value;
        }
    }
}
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class LinkList
    Private idField As Integer
    Private nameField As String
    Private nextField As LinkList
    Private textField() As String
    Public Property id() As Integer
        Get
            Return Me.idField
        End Get
        Set(ByVal value As Integer)
            Me.idField = value
        End Set
    End Property
    Public Property name() As String
        Get
            Return Me.nameField
        End Get
        Set(ByVal value As String)
            Me.nameField = value
        End Set
    End Property
    Public Property [next]() As LinkList
        Get
            Return Me.nextField
        End Get
        Set(ByVal value As LinkList)
            Me.nextField = value
        End Set
    End Property
    <System.Xml.Serialization.XmlTextAttribute()> _
    Public Property Text() As String()
        Get
            Return Me.textField
        End Get
        Set(ByVal value As String())
            Me.textField = value
        End Set
    End Property
End Class

특성을 사용 하는 방법에 대 한 자세한 내용은 참조 하세요. 특성합니다.

참고

더 긴 XmlTextAttribute대신 코드에서 단어를 XmlText 사용할 수 있습니다.

생성자

XmlTextAttribute()

XmlTextAttribute 클래스의 새 인스턴스를 초기화합니다.

XmlTextAttribute(Type)

XmlTextAttribute 클래스의 새 인스턴스를 초기화합니다.

속성

DataType

XmlSerializer에 의해 생성된 텍스트의 XSD(XML 스키마 정의) 데이터 형식을 가져오거나 설정합니다.

Type

멤버 유형을 가져오거나 설정합니다.

TypeId

파생 클래스에서 구현된 경우 이 Attribute에 대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)

메서드

Equals(Object)

이 인스턴스가 지정된 개체와 같은지를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
GetHashCode()

이 인스턴스의 해시 코드를 반환합니다.

(다음에서 상속됨 Attribute)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

파생 클래스에서 재정의된 경우 이 인스턴스 값이 파생 클래스에 대한 기본값인지 여부를 표시합니다.

(다음에서 상속됨 Attribute)
Match(Object)

파생 클래스에서 재정의된 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

(다음에서 상속됨 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

(다음에서 상속됨 Attribute)

적용 대상

추가 정보