XmlTextAttribute Constructors

Definition

Initializes a new instance of the XmlTextAttribute class.

Overloads

XmlTextAttribute()

Initializes a new instance of the XmlTextAttribute class.

XmlTextAttribute(Type)

Initializes a new instance of the XmlTextAttribute class.

XmlTextAttribute()

Source:
XmlTextAttribute.cs
Source:
XmlTextAttribute.cs
Source:
XmlTextAttribute.cs

Initializes a new instance of the XmlTextAttribute class.

public XmlTextAttribute ();

Examples

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

Remarks

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.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

XmlTextAttribute(Type)

Source:
XmlTextAttribute.cs
Source:
XmlTextAttribute.cs
Source:
XmlTextAttribute.cs

Initializes a new instance of the XmlTextAttribute class.

public XmlTextAttribute (Type type);
public XmlTextAttribute (Type? type);

Parameters

type
Type

The Type of the member to be serialized.

Examples

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

Remarks

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.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0