XmlTextAttribute.Type 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置成员的类型。
public:
property Type ^ Type { Type ^ get(); void set(Type ^ value); };
public Type Type { get; set; }
public Type? Type { get; set; }
member this.Type : Type with get, set
Public Property Type As Type
属性值
成员的 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
注解
该 Type 属性只能设置为基元类型和枚举。
XmlTextAttribute也可以应用于返回XmlNode对象或对象的数组的XmlNode字段。
可以应用于 XmlTextAttribute 返回字符串数组的字段或属性。 还可以将属性应用于类型的 Object 数组,但必须将属性设置为 Type 字符串。 在这种情况下,插入数组的任何字符串都序列化为 XML 文本。