.NET Framework Class Library
XmlTextAttribute Class

Indicates to the XmlSerializer that the member must be treated as XML text when the class that contains it is serialized or deserialized.

Inheritance Hierarchy
SystemObject
   SystemAttribute
    System.Xml.SerializationXmlTextAttribute

Namespace:   System.Xml.Serialization
Assemblies:   System.Xml (in System.Xml.dll)
  System.Xml.XmlSerializer (in System.Xml.XmlSerializer.dll)
Syntax
<[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_0_0_0%]([%$TOPIC/ye1exh4y_en-us_VS_110_3_0_0_0_1%].Property Or [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_0_0_2%].Field Or [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_0_0_3%].Parameter Or [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_0_0_4%].ReturnValue)> _
Public Class XmlTextAttribute _
	Inherits [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_0_0_5%]
[[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_1_0_0%]([%$TOPIC/ye1exh4y_en-us_VS_110_3_0_1_0_1%].Property|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_1_0_2%].Field|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_1_0_3%].Parameter|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_1_0_4%].ReturnValue)]
public class XmlTextAttribute : [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_1_0_5%]
[[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_2_0_0%]([%$TOPIC/ye1exh4y_en-us_VS_110_3_0_2_0_1%]::Property|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_2_0_2%]::Field|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_2_0_3%]::Parameter|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_2_0_4%]::ReturnValue)]
public ref class XmlTextAttribute : public [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_2_0_5%]
[<[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_3_0_0%]([%$TOPIC/ye1exh4y_en-us_VS_110_3_0_3_0_1%].Property|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_3_0_2%].Field|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_3_0_3%].Parameter|[%$TOPIC/ye1exh4y_en-us_VS_110_3_0_3_0_4%].ReturnValue)>]
type XmlTextAttribute =  
    class 
        inherit [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_3_0_5%] 
    end
public class XmlTextAttribute extends [%$TOPIC/ye1exh4y_en-us_VS_110_3_0_4_0_0%]

The XmlTextAttribute type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library XmlTextAttributeInitializes a new instance of the XmlTextAttribute class.
Public method Supported by the XNA Framework Supported by Portable Class Library XmlTextAttribute(Type)Initializes a new instance of the XmlTextAttribute class.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework Supported by Portable Class Library DataTypeGets or sets the XML Schema definition language (XSD) data type of the text generated by the XmlSerializer.
Public property Supported by the XNA Framework Supported by Portable Class Library TypeGets or sets the type of the member.
Public property TypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top
Methods
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library EqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method Supported by the XNA Framework Supported by Portable Class Library FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method Supported by the XNA Framework MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method _AttributeGetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeInvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top
Remarks

The XmlTextAttribute belongs to a family of attributes that controls how the XmlSerializer serializes and deserializes an object (through its Serialize and Deserialize methods). For a complete list of similar attributes, see Attributes That Control XML Serialization.

Only one instance of the XmlTextAttribute class can be applied in a class.

You can apply the XmlTextAttribute to public fields and public read/write properties that return primitive and enumeration types.

You can apply the XmlTextAttribute to a field or property that returns an array of strings. You can also apply the attribute to an array of type Object but you must set the Type property to string. In that case, any strings inserted into the array are serialized as XML text.

The XmlTextAttribute can also be applied to a field that returns an XmlNode or an array of XmlNode objects.

By default, the XmlSerializer serializes a class member as an XML element. However, if you apply the XmlTextAttribute to a member, the XmlSerializer translates its value into XML text. This means that the value is encoded into the content of an XML element.

The XML Schema Definition Tool (Xsd.exe) occasionally generates the XmlTextAttribute when creating classes from an XML Schema definition (XSD) file. This occurs when the schema contains a complexType with mixed content; in that case, the corresponding class contains a member that returns a string array to which the XmlTextAttribute is applied. For example, when the Xml Schema Definition tool processes this schema:

 <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>

the following class is generated (extra spaces and remarks have been removed):

<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
[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;
        }
    }
}

For more information about using attributes, see Extending Metadata Using Attributes.

NoteNote

You can use the word XmlText in your code instead of the longer XmlTextAttribute.

Examples
Imports System
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
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();
   }   
}
#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();
}
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.