XmlTextAttribute 类

定义

当串行化或反序列化包含该成员的类时,向 XmlSerializer 指示应将该成员作为 XML 文本处理。

public ref class XmlTextAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlTextAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlTextAttribute = class
    inherit Attribute
Public Class XmlTextAttribute
Inherits Attribute
继承
XmlTextAttribute
属性

示例

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

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

public ref class Group1
{
public:

   // The XmlTextAttribute with type set to string informs the 
   // XmlSerializer that strings should be serialized as XML text.

   [XmlText(String::typeid)]
   [XmlElement(Int32::typeid)]
   [XmlElement(Double::typeid)]
   array<Object^>^All;
   Group1()
   {
      array<Object^>^temp = {321,"One",2,3.0,"Two"};
      All = temp;
   }
};

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

public ref class Group2
{
public:

   [XmlText(Type=GroupType::typeid)]
   GroupType Type;
};

public ref class Group3
{
public:

   [XmlText(Type=DateTime::typeid)]
   DateTime CreationTime;
   Group3()
   {
      CreationTime = DateTime::Now;
   }
};

public ref class Test
{
public:
   static void main()
   {
      Test^ t = gcnew Test;
      t->SerializeArray( "XmlText1.xml" );
      t->SerializeEnum( "XmlText2.xml" );
      t->SerializeDateTime( "XmlText3.xml" );
   }

private:
   void SerializeArray( String^ filename )
   {
      XmlSerializer^ ser = gcnew XmlSerializer( Group1::typeid );
      Group1^ myGroup1 = gcnew Group1;
      TextWriter^ writer = gcnew StreamWriter( filename );
      ser->Serialize( writer, myGroup1 );
      writer->Close();
   }

   void SerializeEnum( String^ filename )
   {
      XmlSerializer^ ser = gcnew XmlSerializer( Group2::typeid );
      Group2^ myGroup = gcnew Group2;
      myGroup->Type = GroupType::Medium;
      TextWriter^ writer = gcnew StreamWriter( filename );
      ser->Serialize( writer, myGroup );
      writer->Close();
   }

   void SerializeDateTime( String^ filename )
   {
      XmlSerializer^ ser = gcnew XmlSerializer( Group3::typeid );
      Group3^ myGroup = gcnew Group3;
      TextWriter^ writer = gcnew StreamWriter( filename );
      ser->Serialize( writer, myGroup );
      writer->Close();
   }
};

int main()
{
   Test::main();
}
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();
   }
}
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

注解

XmlTextAttribute属于一系列属性,这些特性控制 如何XmlSerializer通过Serialize对象的 和 方法) 序列化和Deserialize反序列化对象 (。 有关类似属性的完整列表,请参阅 控制 XML 序列化的属性。

一个类中只能应用类的 XmlTextAttribute 一个实例。

可以将 应用于 XmlTextAttribute 返回基元和枚举类型的公共字段和公共读/写属性。

可以将 应用于 XmlTextAttribute 返回字符串数组的字段或属性。 还可以将 属性应用于 类型的 Object 数组,但必须将 属性设置为 Type 字符串。 在这种情况下,插入数组中的任何字符串都序列化为 XML 文本。

XmlTextAttribute还可以应用于返回 XmlNode 或 对象数组的XmlNode字段。

默认情况下, 将 XmlSerializer 类成员序列化为 XML 元素。 但是,如果将 应用于 XmlTextAttribute 成员, 会 XmlSerializer 将其值转换为 XML 文本。 这意味着该值将编码为 XML 元素的内容。

从 XML 架构定义 (XSD) 文件创建类时,XML 架构定义 工具 (Xsd.exe ) 偶尔会生成 XmlTextAttribute 。 当架构包含 complexType 具有混合内容的 时,就会发生这种情况;在这种情况下,相应的类包含一个成员,该成员返回应用 了 的字符串数组 XmlTextAttribute 。 例如,当工具处理此架构时 Xml Schema Definition

<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>  

以下类是 (多余空格和注释) 删除而生成的:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class LinkList
{

    private int idField;
    private string nameField;
    private LinkList nextField;
    private string[] textField;

    public int id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    public LinkList next
    {
        get
        {
            return this.nextField;
        }
        set
        {
            this.nextField = value;
        }
    }

    [System.Xml.Serialization.XmlTextAttribute()]
    public string[] Text
    {
        get
        {
            return this.textField;
        }
        set
        {
            this.textField = value;
        }
    }
}
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class LinkList
    Private idField As Integer
    Private nameField As String
    Private nextField As LinkList
    Private textField() As String
    Public Property id() As Integer
        Get
            Return Me.idField
        End Get
        Set(ByVal value As Integer)
            Me.idField = value
        End Set
    End Property
    Public Property name() As String
        Get
            Return Me.nameField
        End Get
        Set(ByVal value As String)
            Me.nameField = value
        End Set
    End Property
    Public Property [next]() As LinkList
        Get
            Return Me.nextField
        End Get
        Set(ByVal value As LinkList)
            Me.nextField = value
        End Set
    End Property
    <System.Xml.Serialization.XmlTextAttribute()> _
    Public Property Text() As String()
        Get
            Return Me.textField
        End Get
        Set(ByVal value As String())
            Me.textField = value
        End Set
    End Property
End Class

有关使用特性的详细信息,请参阅 特性

备注

可以在代码中使用 单词 XmlText ,而不是较长 XmlTextAttribute的 。

构造函数

XmlTextAttribute()

初始化 XmlTextAttribute 类的新实例。

XmlTextAttribute(Type)

初始化 XmlTextAttribute 类的新实例。

属性

DataType

获取或设置由 XmlSerializer 生成的文本的 XML 架构定义语言 (XSD) 数据类型。

Type

获取或设置成员的类型。

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅