XmlAttributes.XmlText 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定物件,指定 XmlSerializer 將公用欄位或公用讀取/寫入屬性序列化為 XML 文字。
public:
property System::Xml::Serialization::XmlTextAttribute ^ XmlText { System::Xml::Serialization::XmlTextAttribute ^ get(); void set(System::Xml::Serialization::XmlTextAttribute ^ value); };
public System.Xml.Serialization.XmlTextAttribute XmlText { get; set; }
public System.Xml.Serialization.XmlTextAttribute? XmlText { get; set; }
member this.XmlText : System.Xml.Serialization.XmlTextAttribute with get, set
Public Property XmlText As XmlTextAttribute
屬性值
XmlTextAttribute,覆寫公用屬性或欄位的預設序列化。
範例
下列範例會序列化名為 Group
的類別,其中包含名為 的 Comment
欄位。 若要覆寫序列化欄位的預設方式 XmlSerializer ,此範例會 XmlAttributeOverrides 建立 和 XmlAttributes 物件。 然後,此範例會 XmlTextAttribute 建立 物件,它會指派給 XmlText 屬性,並將物件 (,並將 XmlAttributes 要序列化為 XML 文字的功能變數名稱,) 物件加入 XmlAttributeOverrides 物件。 最後,此範例會 XmlSerializer 使用 XmlAttributeOverrides 物件建立 。
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
// This is the class that will be serialized.
public ref class Group
{
public:
String^ GroupName;
// This field will be serialized as XML text.
String^ Comment;
};
// Return an XmlSerializer to be used for overriding.
XmlSerializer^ CreateOverrider()
{
// Create the XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
XmlAttributes^ xAttrs = gcnew 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 = gcnew XmlTextAttribute;
xAttrs->XmlText = xText;
xOver->Add( Group::typeid, "Comment", xAttrs );
// Create the XmlSerializer, and return it.
return gcnew XmlSerializer( Group::typeid,xOver );
}
void SerializeObject( String^ filename )
{
// Create an instance of the XmlSerializer class.
XmlSerializer^ mySerializer = CreateOverrider();
// Writing the file requires a TextWriter.
TextWriter^ writer = gcnew StreamWriter( filename );
// Create an instance of the class that will be serialized.
Group^ myGroup = gcnew 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();
}
void DeserializeObject( String^ filename )
{
XmlSerializer^ mySerializer = CreateOverrider();
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
Console::WriteLine( myGroup->GroupName );
Console::WriteLine( myGroup->Comment );
}
int main()
{
SerializeObject( "OverrideText.xml" );
DeserializeObject( "OverrideText.xml" );
}
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);
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class Group
Public GroupName As String
' This field will be serialized as XML text.
Public Comment As String
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("OverrideText.xml")
test.DeserializeObject("OverrideText.xml")
End Sub
' Return an XmlSerializer to be used for overriding.
Public Function CreateOverrider() As XmlSerializer
' Create the XmlAttributeOverrides and XmlAttributes objects.
Dim xOver As New XmlAttributeOverrides()
Dim xAttrs As New XmlAttributes()
' Create an XmlTextAttribute and assign it to the XmlText
' property. This instructs the XmlSerializer to treat the
' Comment field as XML text.
Dim xText As New XmlTextAttribute()
xAttrs.XmlText = xText
xOver.Add(GetType(Group), "Comment", xAttrs)
' Create the XmlSerializer, and return it.
Return New XmlSerializer(GetType(Group), xOver)
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create an instance of the XmlSerializer class.
Dim mySerializer As XmlSerializer = CreateOverrider()
' Writing the file requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Create an instance of the class that will be serialized.
Dim myGroup As 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()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Dim mySerializer As XmlSerializer = CreateOverrider()
Dim fs As New FileStream(filename, FileMode.Open)
Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
Console.WriteLine(myGroup.GroupName)
Console.WriteLine(myGroup.Comment)
End Sub
End Class
備註
根據預設,公用欄位或公用讀取/寫入屬性會序列化為 的 XmlSerializer XML 專案。 不過,它可以藉由將 套用 XmlTextAttribute 至欄位或屬性,強制將 欄位或屬性序列化為 XML 文字。
注意
XmlTextAttribute無法套用至傳回陣列的欄位或屬性。
若要覆寫未傳回陣列) 之欄位或 (屬性的預設序列化,請建立 XmlTextAttribute ,並將它指派給 XmlText 物件的 屬性 XmlAttributes 。 將 XmlAttributes 物件新增至 XmlAttributeOverrides 物件,並指定包含覆寫欄位或屬性的物件類型,以及覆寫欄位或屬性的名稱。