次の方法で共有


XmlAttributes.XmlText プロパティ

XmlSerializer に対してパブリック フィールドまたは読み書き可能なパブリック プロパティを XML テキストとしてシリアル化するよう指示するオブジェクトを取得または設定します。

Public Property XmlText As XmlTextAttribute
[C#]
public XmlTextAttribute XmlText {get; set;}
[C++]
public: __property XmlTextAttribute* get_XmlText();public: __property void set_XmlText(XmlTextAttribute*);
[JScript]
public function get XmlText() : XmlTextAttribute;public function set XmlText(XmlTextAttribute);

プロパティ値

パブリック プロパティまたはフィールドの既定のシリアル化をオーバーライドする XmlTextAttribute

解説

既定では、パブリック フィールドまたは読み書き可能なパブリック プロパティは、 XmlSerializer により XML 要素としてシリアル化されます。ただし、このフィールドまたはプロパティに XmlTextAttribute を適用すると、強制的にこのフィールドまたプロパティをシリアル化できます。

メモ   配列を返すフィールドまたはプロパティには、 XmlTextAttribute を適用できません。

配列を返さないフィールドまたはプロパティの既定のシリアル化をオーバーライドするには、 XmlTextAttribute を作成し、それを XmlAttributes オブジェクトの XmlText プロパティに代入します。オーバーライドされたフィールドまたはプロパティを含むオブジェクトの型、およびオーバーライドされたフィールドまたはプロパティの名前を指定して、 XmlAttributes オブジェクトを XmlAttributeOverrides オブジェクトに追加します。

使用例

[Visual Basic, C#, C++] Comment という名前のフィールドを含む Group という名前のクラスをシリアル化する例を次に示します。この例では、XmlSerializer がフィールドをシリアル化する既定の方法をオーバーライドするため、 XmlAttributeOverrides および XmlAttributes オブジェクトを作成します。次に、 XmlTextAttribute オブジェクトを作成して XmlText プロパティに代入し、 XmlAttributes オブジェクトを (XML テキストとしてシリアル化されるフィールドの名前で) 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


[C#] 
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);
   }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.Xml.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 __gc 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 = 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), S"Comment", xAttrs);

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

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 = S".NET";
   myGroup->Comment = S"Great Stuff!";      
   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize(writer, myGroup);
   writer->Close();
}

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

int main()
{
   SerializeObject(S"OverrideText.xml");
   DeserializeObject(S"OverrideText.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 名前空間