다음을 통해 공유


XmlAttributes.XmlText 속성

XmlSerializer가 공용 필드 또는 공용 읽기/쓰기 속성을 XML 텍스트로 serialize하도록 하는 개체를 가져오거나 설정합니다.

네임스페이스: System.Xml.Serialization
어셈블리: System.Xml(system.xml.dll)

구문

‘선언
Public Property XmlText As XmlTextAttribute
‘사용 방법
Dim instance As XmlAttributes
Dim value As XmlTextAttribute

value = instance.XmlText

instance.XmlText = value
public XmlTextAttribute XmlText { get; set; }
public:
property XmlTextAttribute^ XmlText {
    XmlTextAttribute^ get ();
    void set (XmlTextAttribute^ value);
}
/** @property */
public XmlTextAttribute get_XmlText ()

/** @property */
public void set_XmlText (XmlTextAttribute value)
public function get XmlText () : XmlTextAttribute

public function set XmlText (value : XmlTextAttribute)

속성 값

공용 속성 또는 필드의 기본 serialization을 재정의하는 XmlTextAttribute입니다.

설명

기본적으로, 공용 필드 또는 공용 읽기/쓰기 속성은 XmlSerializer를 통해 XML 요소로 serialize됩니다. 그러나, 필드 또는 속성에 XmlTextAttribute를 적용하여 필드 또는 속성이 XML 텍스트로 serialize되도록 할 수 있습니다.

참고

XmlTextAttribute는 배열을 반환하는 필드 또는 속성에 적용할 수 없습니다.

배열을 반환하지 않는 필드 또는 속성의 기본 serialization을 재정의하려면 XmlTextAttribute를 만들어 XmlAttributes 개체의 XmlText 속성에 할당합니다. 그런 다음 XmlAttributes 개체를 XmlAttributeOverrides 개체에 추가하고, 재정의된 필드 또는 속성을 포함하는 개체의 형식 및 재정의된 필드 또는 속성의 이름을 지정합니다.

예제

다음 예제에서는 Comment 필드를 포함하는 Group 클래스를 serialize합니다. 이 예제에서는 XmlSerializer가 필드를 serialize하는 기본 방식을 재정의하기 위해 XmlAttributeOverrides 개체 및 XmlAttributes 개체를 만듭니다. 그런 다음 XmlTextAttribute 개체를 만들어 XmlText 속성에 할당하며, XML 텍스트로 serialize되는 필드의 이름이 있는 XmlAttributes 개체를 XmlAttributeOverrides 개체에 추가합니다. 마지막으로 XmlAttributeOverrides 개체를 사용하여 XmlSerializer를 만듭니다.

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


' This is the class that will be serialized.
Public Class Group
    Public GroupName As String
    
    ' This field will be serialized as XML text. 
    Public Comment As String
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideText.xml")
        test.DeserializeObject("OverrideText.xml")
    End Sub

        
    ' Return an XmlSerializer to be used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects.
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()
        
        ' Create an XmlTextAttribute and assign it to the XmlText
        ' property. This instructs the XmlSerializer to treat the
        ' Comment field as XML text. 
        Dim xText As New XmlTextAttribute()
        xAttrs.XmlText = xText
        xOver.Add(GetType(Group), "Comment", xAttrs)
        
        ' Create the XmlSerializer, and return it.
        Return New XmlSerializer(GetType(Group), xOver)
    End Function
    
    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the class that will be serialized.
        Dim myGroup As New Group()
        
        ' Set the object properties.
        myGroup.GroupName = ".NET"
        myGroup.Comment = "Great Stuff!"
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
        Console.WriteLine(myGroup.GroupName)
        Console.WriteLine(myGroup.Comment)
    End Sub
End Class
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Group
{
   public string GroupName;

   // This field will be serialized as XML text. 
   public string Comment;
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("OverrideText.xml");
      test.DeserializeObject("OverrideText.xml");
   }

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

      /* Create an XmlTextAttribute and assign it to the XmlText 
      property. This instructs the XmlSerializer to treat the 
      Comment field as XML text. */      
      XmlTextAttribute xText = new XmlTextAttribute();
      xAttrs.XmlText = xText;
      xOver.Add(typeof(Group), "Comment", xAttrs);

      // Create the XmlSerializer, and return it.
      return new XmlSerializer(typeof(Group), xOver);
   }
   
 
   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrider();
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";
      myGroup.Comment = "Great Stuff!";      
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group) 
      mySerializer.Deserialize(fs);
      Console.WriteLine(myGroup.GroupName);
      Console.WriteLine(myGroup.Comment);
   }
}
   
#using <System.Xml.dll>
#using <System.dll>

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

// This is the class that will be serialized.
public ref class Group
{
public:
   String^ GroupName;

   // This field will be serialized as XML text. 
   String^ Comment;
};

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

   /* Create an XmlTextAttribute and assign it to the XmlText 
      property. This instructs the XmlSerializer to treat the 
      Comment field as XML text. */
   XmlTextAttribute^ xText = gcnew XmlTextAttribute;
   xAttrs->XmlText = xText;
   xOver->Add( Group::typeid, "Comment", xAttrs );

   // Create the XmlSerializer, and return it.
   return gcnew XmlSerializer( Group::typeid,xOver );
}

void SerializeObject( String^ filename )
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = CreateOverrider();

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew Group;

   // Set the object properties.
   myGroup->GroupName = ".NET";
   myGroup->Comment = "Great Stuff!";

   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize( writer, myGroup );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
   Console::WriteLine( myGroup->GroupName );
   Console::WriteLine( myGroup->Comment );
}

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

// This is the class that will be serialized.
public class Group
{
    public String groupName;
    // This field will be serialized as XML text. 
    public String comment;
} //Group

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("OverrideText.xml");
        test.DeserializeObject("OverrideText.xml");
    } //main

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

        /* Create an XmlTextAttribute and assign it to the XmlText 
           property. This instructs the XmlSerializer to treat the 
           Comment field as XML text. */
        XmlTextAttribute xText = new XmlTextAttribute();
        xAttrs.set_XmlText(xText);
        xOver.Add(Group.class.ToType(), "Comment", xAttrs);

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

    public void SerializeObject(String fileName)
    {
        // Create an instance of the XmlSerializer class.
        XmlSerializer mySerializer = CreateOverrider();

        // Writing the file requires a TextWriter.
        TextWriter writer = new StreamWriter(fileName);

        // Create an instance of the class that will be serialized.
        Group myGroup = new Group();

        // Set the object properties.
        myGroup.groupName = ".NET";
        myGroup.comment = "Great Stuff!";

        // Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlSerializer mySerializer = CreateOverrider();
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup = (Group)mySerializer.Deserialize(fs);

        Console.WriteLine(myGroup.groupName);
        Console.WriteLine(myGroup.comment);
    } //DeserializeObject
} //Run

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

XmlAttributes 클래스
XmlAttributes 멤버
System.Xml.Serialization 네임스페이스