XmlElementAttribute.Type-Eigenschaft
Ruft den Objekttyp ab, mit dem das XML-Element dargestellt wird, oder legt diesen fest.
Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Property Type As Type
'Usage
Dim instance As XmlElementAttribute
Dim value As Type
value = instance.Type
instance.Type = value
public Type Type { get; set; }
public:
property Type^ Type {
Type^ get ();
void set (Type^ value);
}
/** @property */
public Type get_Type ()
/** @property */
public void set_Type (Type value)
public function get Type () : Type
public function set Type (value : Type)
Eigenschaftenwert
Der Type des Members.
Hinweise
Geben Sie mithilfe der Type-Eigenschaft einen abgeleiteten Typ für ein Feld oder eine Eigenschaft an.
Wenn ein Feld bzw. eine Eigenschaft ArrayList zurückgibt, weisen Sie dem Member mehrere Instanzen von XmlElementAttribute zu. Legen Sie für jede Instanz die Type-Eigenschaft auf einen Objekttyp fest, der in das Array eingefügt werden kann.
Beispiel
Im folgenden Beispiel wird mithilfe der Type-Eigenschaft ein abgeleitetes Objekt für XmlElementAttribute angegeben. Darüber hinaus werden drei Instanzen von XmlElementAttribute auf ein Feld angewendet, das ArrayList zurückgibt. Jede Instanz gibt einen Typ an, der im Feld zulässig ist.
Imports System
Imports System.Collections
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Group
<XmlElement(GetType(Manager))> _
Public Staff() As Employee
<XmlElement(GetType(Integer)), _
XmlElement(GetType(String)), _
XmlElement(GetType(DateTime))> _
Public ExtraInfo As ArrayList
End Class
Public Class Employee
Public Name As String
End Class
Public Class Manager
Inherits Employee
Public Level As Integer
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("TypeEx.xml")
End Sub
Public Sub SerializeObject(filename As String)
' Create an XmlSerializer instance.
Dim xSer As New XmlSerializer(GetType(Group))
' Create an object and serialize it.
Dim myGroup As New Group()
Dim e1 As New Manager()
e1.Name = "Manager1"
Dim m1 As New Manager()
m1.Name = "Manager2"
m1.Level = 4
Dim emps() As Employee = {e1, m1}
myGroup.Staff = emps
myGroup.ExtraInfo = New ArrayList()
myGroup.ExtraInfo.Add(".NET")
myGroup.ExtraInfo.Add(42)
myGroup.ExtraInfo.Add(New DateTime(2001, 1, 1))
Dim writer As New StreamWriter(filename)
xSer.Serialize(writer, myGroup)
writer.Close()
End Sub
End Class
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Group
{
[XmlElement(typeof(Manager))]
public Employee [] Staff;
[XmlElement (typeof(int)),
XmlElement (typeof(string)),
XmlElement (typeof(DateTime))]
public ArrayList ExtraInfo;
}
public class Employee
{
public string Name;
}
public class Manager:Employee
{
public int Level;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("TypeEx.xml");
}
public void SerializeObject(string filename)
{
// Create an XmlSerializer instance.
XmlSerializer xSer =
new XmlSerializer(typeof(Group));
// Create object and serialize it.
Group myGroup = new Group();
Manager e1 = new Manager();
e1.Name = "Manager1";
Manager m1 = new Manager();
m1.Name = "Manager2";
m1.Level = 4;
Employee[] emps = {e1, m1};
myGroup.Staff = emps;
myGroup.ExtraInfo = new ArrayList();
myGroup.ExtraInfo.Add(".NET");
myGroup.ExtraInfo.Add(42);
myGroup.ExtraInfo.Add(new DateTime(2001,1,1));
TextWriter writer = new StreamWriter(filename);
xSer.Serialize(writer, myGroup);
writer.Close();
}
}
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Employee
{
public:
String^ Name;
};
public ref class Manager: public Employee
{
public:
int Level;
};
public ref class Group
{
public:
[XmlElement(Manager::typeid)]
array<Employee^>^Staff;
[XmlElement(Int32::typeid),
XmlElement(String::typeid),
XmlElement(DateTime::typeid)]
ArrayList^ ExtraInfo;
};
void SerializeObject( String^ filename )
{
// Create an XmlSerializer instance.
XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid );
// Create object and serialize it.
Group^ myGroup = gcnew Group;
Manager^ e1 = gcnew Manager;
e1->Name = "Manager1";
Manager^ m1 = gcnew Manager;
m1->Name = "Manager2";
m1->Level = 4;
array<Employee^>^emps = {e1,m1};
myGroup->Staff = emps;
myGroup->ExtraInfo = gcnew ArrayList;
myGroup->ExtraInfo->Add( ".NET" );
myGroup->ExtraInfo->Add( 42 );
myGroup->ExtraInfo->Add( DateTime(2001,1,1) );
TextWriter^ writer = gcnew StreamWriter( filename );
xSer->Serialize( writer, myGroup );
writer->Close();
}
int main()
{
SerializeObject( "TypeEx.xml" );
}
import System.*;
import System.Collections.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;
public class Group
{
/** @attribute XmlElement(Manager.class)
*/
public Employee staff[];
/** @attribute XmlElement(int.class)
* @attribute XmlElement(String.class)
* @attribute XmlElement(DateTime.class)
*/
public ArrayList extraInfo;
} //Group
public class Employee
{
public String name;
} //Employee
public class Manager extends Employee
{
public int level;
} //Manager
public class Run
{
public static void main(String[] args)
{
Run test = new Run();
test.SerializeObject("TypeEx.xml");
} //main
public void SerializeObject(String fileName)
{
// Create an XmlSerializer instance.
XmlSerializer xSer = new XmlSerializer(Group.class.ToType());
// Create object and serialize it.
Group myGroup = new Group();
Manager e1 = new Manager();
e1.name = "Manager1";
Manager m1 = new Manager();
m1.name = "Manager2";
m1.level = 4;
Employee emps[] = { e1, m1 };
myGroup.staff = emps;
myGroup.extraInfo = new ArrayList();
myGroup.extraInfo.Add(".NET");
myGroup.extraInfo.Add((Int32)42);
myGroup.extraInfo.Add(new DateTime(2001, 1, 1));
TextWriter writer = new StreamWriter(fileName);
xSer.Serialize(writer, myGroup);
writer.Close();
} //SerializeObject
} //Run
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
XmlElementAttribute-Klasse
XmlElementAttribute-Member
System.Xml.Serialization-Namespace