XmlTextAttribute Constructor

Initializes a new instance of the XmlTextAttribute class.

Namespace:  System.Xml.Serialization
Assemblies:   System.Xml.XmlSerializer (in System.Xml.XmlSerializer.dll)
  System.Xml (in System.Xml.dll)

public XmlTextAttribute()

You can override the way that the XmlSerializer serializes a public field or public read/write property by creating an XmlAttributes, and setting its XmlText property to an XmlTextAttribute. For more details, see the XmlAttributeOverrides class.

The following example serializes a class that contains a public field, named Comment. The example applies an XmlTextAttribute to the field, thereby overriding its serialization as an XML element, and instead serializing it as XML text.

using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;


public class Group {
    public string GroupName;
    public string Comment;
}

public class Test {
    public static void Main() {
        Test t = new Test();
        t.SerializerOrder("TextOverride.xml");
    }
    /* Create an instance of the XmlSerializer class that overrides 
       the default way it serializes an object. */ 
    public XmlSerializer CreateOverrider() {
        /* Create instances of the XmlAttributes and 
        XmlAttributeOverrides classes. */

        XmlAttributes attrs = new XmlAttributes();

        XmlAttributeOverrides xOver = new XmlAttributeOverrides();

        /* Create an XmlTextAttribute to override the default 
        serialization process. */
        XmlTextAttribute xText = new XmlTextAttribute();
        attrs.XmlText = xText;

        // Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(typeof(Group), "Comment", attrs);

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

    public void SerializerOrder(string filename) {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Group myGroup = new Group();
        myGroup.Comment = "This is a great product.";

        TextWriter writer = new StreamWriter(filename);
        xSer.Serialize(writer, myGroup);
    }
}
   

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft