Export (0) Print
Expand All

XmlAttributes.XmlText Property

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)

public XmlTextAttribute XmlText { get; set; }
/** @property */
public XmlTextAttribute get_XmlText ()

/** @property */
public void set_XmlText (XmlTextAttribute value)

public function get XmlText () : XmlTextAttribute

public function set XmlText (value : XmlTextAttribute)

Not applicable.

Property Value

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.

NoteNote:

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;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Group
{
   public string GroupName;

   // This field will be serialized as XML text. 
   public string Comment;
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("OverrideText.xml");
      test.DeserializeObject("OverrideText.xml");
   }

   // Return an XmlSerializer to be used for overriding. 
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      XmlAttributes xAttrs = new 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 = new XmlTextAttribute();
      xAttrs.XmlText = xText;
      xOver.Add(typeof(Group), "Comment", xAttrs);

      // Create the XmlSerializer, and return it.
      return new XmlSerializer(typeof(Group), xOver);
   }
   
 
   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrider();
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";
      myGroup.Comment = "Great Stuff!";      
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group) 
      mySerializer.Deserialize(fs);
      Console.WriteLine(myGroup.GroupName);
      Console.WriteLine(myGroup.Comment);
   }
}
   

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// This is the class that will be serialized.
public class Group
{
    public String groupName;
    // This field will be serialized as XML text. 
    public String comment;
} //Group

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("OverrideText.xml");
        test.DeserializeObject("OverrideText.xml");
    } //main

    // Return an XmlSerializer to be used for overriding. 
    public XmlSerializer CreateOverrider()
    {
        // Create the XmlAttributeOverrides and XmlAttributes objects.
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
        XmlAttributes xAttrs = new 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 = new XmlTextAttribute();
        xAttrs.set_XmlText(xText);
        xOver.Add(Group.class.ToType(), "Comment", xAttrs);

        // Create the XmlSerializer, and return it.
        return new XmlSerializer(Group.class.ToType(), xOver);
    } //CreateOverrider

    public void SerializeObject(String fileName)
    {
        // Create an instance of the XmlSerializer class.
        XmlSerializer mySerializer = CreateOverrider();

        // Writing the file requires a TextWriter.
        TextWriter writer = new StreamWriter(fileName);

        // Create an instance of the class that will be serialized.
        Group myGroup = new Group();

        // Set the object properties.
        myGroup.groupName = ".NET";
        myGroup.comment = "Great Stuff!";

        // Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlSerializer mySerializer = CreateOverrider();
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup = (Group)mySerializer.Deserialize(fs);

        Console.WriteLine(myGroup.groupName);
        Console.WriteLine(myGroup.comment);
    } //DeserializeObject
} //Run

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft