XmlAttributes::XmlText Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets an object that instructs the XmlSerializer to serialize a public field or public read/write property as XML text.

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

property XmlTextAttribute^ XmlText {
	XmlTextAttribute^ get();
	void set(XmlTextAttribute^ value);

Property Value

Type: System.Xml.Serialization::XmlTextAttribute^

An XmlTextAttribute that overrides the default serialization of a public property or field.

By default, a public field or public read/write property is serialized as an XML element by the XmlSerializer. However, it can force the field or property to be serialized as XML text by applying an XmlTextAttribute to the field or property.


The XmlTextAttribute cannot be applied to a field or property that returns an array.

To override the default serialization of a field or property (that does not return an array), create an XmlTextAttribute and assign it to the XmlText property of an XmlAttributes object. Add the XmlAttributes object to an XmlAttributeOverrides object and specify the type of the object that contains the overridden field or property, and the name of the overridden field or property.

The following example serializes the class named Group, which contains a field named Comment. To override the default way the XmlSerializer serializes the field, the example creates an XmlAttributeOverrides and an XmlAttributes object. The example then creates an XmlTextAttribute object, which it assigns to the XmlText property, and adds the XmlAttributes object (with the name of the field to be serialized as XML text) to the XmlAttributeOverrides object. Lastly the example creates an XmlSerializer using the XmlAttributeOverrides object.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

// This is the class that will be serialized.
public ref class Group
   String^ GroupName;

   // This field will be serialized as XML text. 
   String^ Comment;

// Return an XmlSerializer to be used for overriding. 
XmlSerializer^ CreateOverrider()
   // Create the XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
   XmlAttributes^ xAttrs = gcnew XmlAttributes;

   /* Create an XmlTextAttribute and assign it to the XmlText 
      property. This instructs the XmlSerializer to treat the 
      Comment field as XML text. */
   XmlTextAttribute^ xText = gcnew XmlTextAttribute;
   xAttrs->XmlText = xText;
   xOver->Add( Group::typeid, "Comment", xAttrs );

   // Create the XmlSerializer, and return it.
   return gcnew XmlSerializer( Group::typeid,xOver );

void SerializeObject( String^ filename )
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = CreateOverrider();

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew Group;

   // Set the object properties.
   myGroup->GroupName = ".NET";
   myGroup->Comment = "Great Stuff!";

   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize( writer, myGroup );

void DeserializeObject( String^ filename )
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
   Console::WriteLine( myGroup->GroupName );
   Console::WriteLine( myGroup->Comment );

int main()
   SerializeObject( "OverrideText.xml" );
   DeserializeObject( "OverrideText.xml" );

Universal Windows Platform
Available since 8
.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
Return to top