Export (0) Print
Expand All

XmlTextAttribute Class

Indicates to the XmlSerializer that the member should be treated as XML text when the containing class is serialized or deserialized.

For a list of all members of this type, see XmlTextAttribute Members.

System.Object
   System.Attribute
      System.Xml.Serialization.XmlTextAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field _
   Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
Public Class XmlTextAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field
   | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class XmlTextAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Property |
   AttributeTargets::Field | AttributeTargets::Parameter |
   AttributeTargets::ReturnValue)]
public __gc class XmlTextAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Property | AttributeTargets.Field |
   AttributeTargets.Parameter | AttributeTargets.ReturnValue)
class XmlTextAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The XmlTextAttribute belongs to a family of attributes that controls how the XmlSerializer serializes and deserializes an object (through its Serialize and Deserialize methods). See Attributes That Control XML Serialization for a complete list of similar attributes.

Only one instance of the XmlTextAttribute class can be applied in a class.

You can apply the XmlTextAttribute to public fields and public read/write properties that return primitive and enumeration types.

You can apply the XmlTextAttribute to a field or property that returns an array of strings. You can also apply the attribute to an array of type Object but you must set the Type property to string. In that case, any strings inserted into the array will be serialized as XML text.

The XmlTextAttribute can also be applied to a field that returns an XmlNode or an array of XmlNode objects.

By default, the XmlSerializer serializes a class member as an XML element. However, if you apply the XmlTextAttribute to a member, the XmlSerializer translates its value into XML text. This means that the value will be encoded into the content of an XML element.

The XML Schema Definition Tool (Xsd.exe) occasionally generates the XmlTextAttribute when creating classes from an XML Schema definition (XSD) file. This occurs when the schema contains a complexType with mixed content; in that case, the corresponding class will contain a member that returns a string array to which the XmlTextAttribute is applied. For example, when the Xml Schema Definition tool processes this schema:

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="LinkList" type="LinkList" />
  <xs:complexType name="LinkList" mixed="true">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="id" type="xs:int" />
      <xs:element minOccurs="0" maxOccurs="1" name="name" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="next" type="LinkList" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

the following class will be generated:

' Visual Basic code
Public Class LinkList 
   Public id As Integer
   Public string name
   Public LinkList next
   <System.Xml.Serialization.XmlTextAttribute()> _
   Public Text() As string
End Class
// C# code
public class LinkList {
   public int id;
   public string name;
   public LinkList next;
   [System.Xml.Serialization.XmlTextAttribute()]
   public string[] Text;
}

For more information about using attributes, see Extending Metadata Using Attributes.

Note   In your code, you can use the word XmlText instead of the longer XmlTextAttribute.

Example

[Visual Basic] 
Imports System
Imports System.Xml.Serialization
Imports System.IO


Public Class Group1
   ' The XmlTextAttribute with type set to String informs the 
   ' XmlSerializer that strings should be serialized as XML text.
   <XmlText(GetType(String)), _
   XmlElement(GetType(integer)), _  
   XmlElement(GetType(double))> _
   public All () As Object = _
   New Object (){321, "One", 2, 3.0, "Two" }
End Class


Public Class Group2
   <XmlText(GetType(GroupType))> _
   public Type As GroupType 
End Class

Public Enum GroupType
   Small
   Medium
   Large
End Enum

Public Class Group3
   <XmlText(GetType(DateTime))> _
   Public CreationTime As DateTime = DateTime.Now
End Class

Public Class Test
   Shared Sub Main()
      Dim t As Test = New Test()
      t.SerializeArray("XmlText1.xml")
      t.SerializeEnum("XmlText2.xml")
      t.SerializeDateTime("XmlText3.xml")
   End Sub

   Private Sub SerializeArray(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group1))
      Dim myGroup1 As Group1 = New Group1()

      Dim writer As TextWriter = New StreamWriter(filename)

      ser.Serialize(writer, myGroup1)
      writer.Close()
   End Sub

   Private Sub SerializeEnum(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group2))
      Dim myGroup As Group2 = New Group2()
      myGroup.Type = GroupType.Medium
      Dim writer As TextWriter = New StreamWriter(filename)

      ser.Serialize(writer, myGroup)
      writer.Close()
   End Sub

   Private Sub SerializeDateTime(filename As String)
      Dim ser As XmlSerializer = new XmlSerializer(GetType(Group3))
      Dim myGroup As Group3 = new Group3()
      Dim writer As TextWriter = new StreamWriter(filename)

      ser.Serialize(writer, myGroup)
      writer.Close()
   End Sub
End Class

[C#] 
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();
   }   
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::Xml::Serialization;
using namespace System::IO;


public __gc class Group1{
   // The XmlTextAttribute with type set to string informs the 
   // XmlSerializer that strings should be serialized as XML text.
public:
   [XmlText(__typeof(String))]
   [XmlElement(__typeof(Int32))]  
   [XmlElement(__typeof(Double))]
   Object* All[];
   Group1(){
      Object* temp[]= {__box(321), S"One", __box(2), __box(3.0), S"Two" };
      All = temp;
   }
};

public __value enum GroupType{
   Small,
   Medium,
   Large
};

public __gc class Group2{
public:
   [XmlText(Type = __typeof(GroupType))]
   GroupType Type;
};

public __gc class Group3{
public:
   [XmlText(Type=__typeof(DateTime))]
   DateTime CreationTime;
   Group3(){
      CreationTime = DateTime::Now;
   }
};

public __gc class Test{
public:
   static void main(){
      Test* t = new Test();
      t->SerializeArray(S"XmlText1.xml");
      t->SerializeEnum(S"XmlText2.xml");
      t->SerializeDateTime(S"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();
   }

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

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

int main()
{
   Test::main();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Xml.Serialization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

XmlTextAttribute Members | System.Xml.Serialization Namespace | XmlAttributeOverrides | XmlAttributes | XmlSerializer | XmlText | Introducing XML Serialization | Overriding XML Serialization | XmlAttributes | Controlling XML Serialization Using Attributes | Examples of XML Serialization

Show:
© 2014 Microsoft