XmlTextAttribute Constructor ()

 

Initializes a new instance of the XmlTextAttribute class.

Namespace:   System.Xml.Serialization
Assembly:  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);
    }
}

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: