XmlTextAttribute Class
Collapse the table of content
Expand the table of content

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.

Namespace:   System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)


[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class XmlTextAttribute : Attribute


Initializes a new instance of the XmlTextAttribute class.


Initializes a new instance of the XmlTextAttribute class.


Gets or sets the XML Schema definition language (XSD) data type of the text generated by the XmlSerializer.


Gets or sets the type of the member.


When implemented in a derived class, gets a unique identifier for this Attribute.(Inherited from Attribute.)


This API supports the product infrastructure and is not intended to be used directly from your code. Returns a value that indicates whether this instance is equal to a specified object.(Inherited from Attribute.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Returns the hash code for this instance.(Inherited from Attribute.)


Gets the Type of the current instance.(Inherited from Object.)


When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.(Inherited from Attribute.)


When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.(Inherited from Attribute.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

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

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Attribute.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can be used to get the type information for an interface.(Inherited from Attribute.)


Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Attribute.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.(Inherited from Attribute.)

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="" 
  <xs:element name="LinkList" type="LinkList" />
  <xs:complexType name="LinkList" mixed="true">
      <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" />

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

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[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
            return this.idField;
            this.idField = value;

    public string name
            return this.nameField;
            this.nameField = value;

    public LinkList next
            return this.nextField;
            this.nextField = value;

    public string[] Text
            return this.textField;
            this.textField = value;

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


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

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.
   public object [] All= new object []{321, "One", 2, 3.0, "Two" };

public class Group2{
   [XmlText(Type = typeof(GroupType))]
   public GroupType Type;
public enum GroupType{

public class Group3{
   public DateTime CreationTime = DateTime.Now;

public class Test{
   static void Main(){
      Test t = new Test();

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

      TextWriter writer = new StreamWriter(filename);

      ser.Serialize(writer, myGroup1);

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

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

      ser.Serialize(writer, myGroup);

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft