XmlTextAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示 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
- 繼承
- 屬性
範例
#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 架構定義工具 (#A) 0 偶爾會產生 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) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取物件的類型資訊,可以用來取得介面的類型資訊。 (繼承來源 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開的屬性和方法的存取權。 (繼承來源 Attribute) |