XmlTextAttribute 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 XmlTextAttribute 類別的新執行個體。
多載
XmlTextAttribute() |
初始化 XmlTextAttribute 類別的新執行個體。 |
XmlTextAttribute(Type) |
初始化 XmlTextAttribute 類別的新執行個體。 |
XmlTextAttribute()
初始化 XmlTextAttribute 類別的新執行個體。
public:
XmlTextAttribute();
public XmlTextAttribute ();
Public Sub New ()
範例
下列範例會序列化類別,其中包含名為 的 Comment
公用欄位。 此範例會將 XmlTextAttribute 套用至 欄位,藉此覆寫其序列化為 XML 元素,並改為將它序列化為 XML 文字。
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::Xml::Schema;
public ref class Group
{
public:
String^ GroupName;
String^ Comment;
};
public ref class Test
{
public:
static void main()
{
Test^ t = gcnew Test;
t->SerializerOrder( "TextOverride.xml" );
}
/* Create an instance of the XmlSerializer class that overrides
the default way it serializes an object. */
XmlSerializer^ CreateOverrider()
{
/* Create instances of the XmlAttributes and
XmlAttributeOverrides classes. */
XmlAttributes^ attrs = gcnew XmlAttributes;
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
/* Create an XmlTextAttribute to override the default
serialization process. */
XmlTextAttribute^ xText = gcnew XmlTextAttribute;
attrs->XmlText = xText;
// Add the XmlAttributes to the XmlAttributeOverrides.
xOver->Add( Group::typeid, "Comment", attrs );
// Create the XmlSerializer, and return it.
XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
return xSer;
}
void SerializerOrder( String^ filename )
{
// Create an XmlSerializer instance.
XmlSerializer^ xSer = CreateOverrider();
// Create the object and serialize it.
Group^ myGroup = gcnew Group;
myGroup->Comment = "This is a great product.";
TextWriter^ writer = gcnew StreamWriter( filename );
xSer->Serialize( writer, myGroup );
}
};
int main()
{
Test::main();
}
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
public class Group {
public string GroupName;
public string Comment;
}
public class Test {
public static void Main() {
Test t = new Test();
t.SerializerOrder("TextOverride.xml");
}
/* Create an instance of the XmlSerializer class that overrides
the default way it serializes an object. */
public XmlSerializer CreateOverrider() {
/* Create instances of the XmlAttributes and
XmlAttributeOverrides classes. */
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
/* Create an XmlTextAttribute to override the default
serialization process. */
XmlTextAttribute xText = new XmlTextAttribute();
attrs.XmlText = xText;
// Add the XmlAttributes to the XmlAttributeOverrides.
xOver.Add(typeof(Group), "Comment", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
return xSer;
}
public void SerializerOrder(string filename) {
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object and serialize it.
Group myGroup = new Group();
myGroup.Comment = "This is a great product.";
TextWriter writer = new StreamWriter(filename);
xSer.Serialize(writer, myGroup);
}
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml.Schema
Imports System.Xml
Public Class Group
Public GroupName As String
Public Comment As String
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
t.SerializerOrder("TextOverride.xml")
End Sub
' Create an instance of the XmlSerializer class that overrides
' the default way it serializes an object.
Public Function CreateOverrider() As XmlSerializer
' CreatE instances of the XmlAttributes and
' XmlAttributeOverrides classes.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
' Create an XmlTextAttribute to override the default
' serialization process.
Dim xText As New XmlTextAttribute()
attrs.XmlText = xText
' Add the XmlAttributes to the XmlAttributeOverrides.
xOver.Add(GetType(Group), "Comment", attrs)
' Create the XmlSerializer, and return it.
Dim xSer As New XmlSerializer(GetType(Group), xOver)
Return xSer
End Function
Public Sub SerializerOrder(ByVal filename As String)
' Create an XmlSerializer instance.
Dim xSer As XmlSerializer = CreateOverrider()
' Create the object and serialize it.
Dim myGroup As New Group()
myGroup.Comment = "This is a great product."
Dim writer As New StreamWriter(filename)
xSer.Serialize(writer, myGroup)
End Sub
End Class
備註
您可以藉由建立 XmlAttributes ,並將其屬性設定 XmlText 為 XmlTextAttribute ,覆寫序列化公用欄位或公用讀取/寫入屬性的方式 XmlSerializer 。 如需詳細資訊,請參閱 XmlAttributeOverrides 類別。
適用於
XmlTextAttribute(Type)
初始化 XmlTextAttribute 類別的新執行個體。
public:
XmlTextAttribute(Type ^ type);
public XmlTextAttribute (Type type);
public XmlTextAttribute (Type? type);
new System.Xml.Serialization.XmlTextAttribute : Type -> System.Xml.Serialization.XmlTextAttribute
Public Sub New (type As Type)
參數
範例
#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
備註
您可以藉由建立 XmlAttributes ,並將其屬性設定 XmlText 為 XmlTextAttribute ,覆寫序列化公用欄位或公用讀取/寫入屬性的方式 XmlSerializer 。 如需詳細資訊,請參閱 XmlAttributeOverrides 類別。