XmlTextAttribute Class
Assembly: System.Xml (in system.xml.dll)
'Declaration <AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)> _ Public Class XmlTextAttribute Inherits Attribute 'Usage Dim instance As XmlTextAttribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue) */ public class XmlTextAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue) public class XmlTextAttribute extends Attribute
The XmlTextAttribute belongs to a family of attributes that controls how the XmlSerializer serializes and deserializes an object (through its Serialize and Deserialize methods). For a complete list of similar attributes, see Attributes That Control XML Serialization.
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 are 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 is 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 contains 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 is 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 |
|---|
| You can use the word XmlText in your code instead of the longer XmlTextAttribute. |
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
import System.*;
import System.Xml.Serialization.*;
import System.IO.*;
public class Group1
{
// The XmlTextAttribute with type set to string informs the
// XmlSerializer that strings should be serialized as XML text.
/** @attribute XmlText(String.class)
*/
/** @attribute XmlElement(int.class)
*/
/** @attribute XmlElement(double.class)
*/
public Object all[] = new Object[] { (Int32)321, "One", (Int32)2,
(System.Double)3.0, "Two" };
} //Group1
public class Group2
{
/** @attribute XmlElement(GroupType.class, ElementName = "GroupType")
*/
public GroupType type;
} //Group2
public class GroupType
{
private int member;
GroupType()
{
member = 0;
} //GroupType
GroupType(int n)
{
member = n;
} //GroupType
public static int small;
public static int medium;
public static int large;
} //GroupType
public class Group3
{
/** @attribute XmlText(Type = DateTime.class)
*/
public DateTime creationTime = DateTime.get_Now();
} //Group3
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.SerializeArray("XmlText1.xml");
t.SerializeEnum("XmlText2.xml");
t.SerializeDateTime("XmlText3.xml");
} //main
private void SerializeArray(String fileName)
{
XmlSerializer ser = new XmlSerializer(Group1.class.ToType());
Group1 myGroup1 = new Group1();
TextWriter writer = new StreamWriter(fileName);
ser.Serialize(writer, myGroup1);
writer.Close();
} //SerializeArray
private void SerializeEnum(String fileName)
{
XmlSerializer ser = new XmlSerializer(Group2.class.ToType());
Group2 myGroup = new Group2();
myGroup.type = new GroupType(GroupType.medium);
TextWriter writer = new StreamWriter(fileName);
ser.Serialize(writer, myGroup);
writer.Close();
} //SerializeEnum
private void SerializeDateTime(String fileName)
{
XmlSerializer ser = new XmlSerializer(Group3.class.ToType());
Group3 myGroup = new Group3();
TextWriter writer = new StreamWriter(fileName);
ser.Serialize(writer, myGroup);
writer.Close();
} //SerializeDateTime
} //Test
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
XmlTextAttribute MembersSystem.Xml.Serialization Namespace
XmlAttributeOverrides Class
XmlAttributes Class
XmlSerializer Class
XmlAttributes.XmlText Property
XmlAttributes Class
Other Resources
Introducing XML SerializationHow to: Specify an Alternate Element Name for an XML Stream
Controlling XML Serialization Using Attributes
Examples of XML Serialization
XML Schema Definition Tool (Xsd.exe)
Note