XmlIncludeAttribute.Type Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the type of the object to include.
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
The Type of the object to include.
The following example defines a class named Group
, which contains a field named Employees
that returns an array of Employee
objects. The example derives the Manager
class from the Employee
class, and applies the XmlIncludeAttribute to the Employee
class. When the example creates a Group
object, it inserts a Manager
object into the Employee
array. Lastly, the example serializes the Group
object.
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
ref class Employee;
ref class Manager;
ref class Group
{
public:
array<Employee^>^Employees;
};
// Instruct the XmlSerializer to accept Manager types as well.
[XmlInclude(Manager::typeid)]
public ref class Employee
{
public:
String^ Name;
};
public ref class Manager: public Employee
{
public:
int Level;
};
void SerializeObject( String^ filename )
{
XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );
TextWriter^ writer = gcnew StreamWriter( filename );
Group^ group = gcnew Group;
Manager^ manager = gcnew Manager;
Employee^ emp1 = gcnew Employee;
Employee^ emp2 = gcnew Employee;
manager->Name = "Zeus";
manager->Level = 2;
emp1->Name = "Ares";
emp2->Name = "Artemis";
array<Employee^>^emps = {manager,emp1,emp2};
group->Employees = emps;
s->Serialize( writer, group );
writer->Close();
}
void DeserializeObject( String^ filename )
{
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
XmlSerializer^ x = gcnew XmlSerializer( Group::typeid );
Group^ g = dynamic_cast<Group^>(x->Deserialize( fs ));
Console::Write( "Members:" );
System::Collections::IEnumerator^ myEnum = g->Employees->GetEnumerator();
while ( myEnum->MoveNext() )
{
Employee^ e = safe_cast<Employee^>(myEnum->Current);
Console::WriteLine( "\t{0}", e->Name );
}
}
int main()
{
SerializeObject( "IncludeExample.xml" );
DeserializeObject( "IncludeExample.xml" );
}
using System;
using System.IO;
using System.Xml.Serialization;
public class Group
{
public Employee[] Employees;
}
// Instruct the XmlSerializer to accept Manager types as well.
[XmlInclude(typeof(Manager))]
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("IncludeExample.xml");
test.DeserializeObject("IncludeExample.xml");
}
public void SerializeObject(string filename)
{
XmlSerializer s = new XmlSerializer(typeof(Group));
TextWriter writer = new StreamWriter(filename);
Group group = new Group();
Manager manager = new Manager();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
manager.Name = "Zeus";
manager.Level = 2;
emp1.Name = "Ares";
emp2.Name = "Artemis";
Employee [] emps = new Employee[3]{manager, emp1, emp2};
group.Employees = emps;
s.Serialize(writer, group);
writer.Close();
}
public void DeserializeObject(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
XmlSerializer x = new XmlSerializer(typeof(Group));
Group g = (Group) x.Deserialize(fs);
Console.Write("Members:");
foreach(Employee e in g.Employees)
{
Console.WriteLine("\t" + e.Name);
}
}
}
Imports System.IO
Imports System.Xml.Serialization
Public Class Group
Public Employees() As Employee
End Class
' Instruct the XmlSerializer to accept Manager types as well.
<XmlInclude(GetType(Manager))> _
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("IncludeExample.xml")
test.DeserializeObject("IncludeExample.xml")
End Sub
Public Sub SerializeObject(ByVal filename As String)
Dim s As New XmlSerializer(GetType(Group))
Dim writer As New StreamWriter(filename)
Dim group As New Group()
Dim manager As New Manager()
Dim emp1 As New Employee()
Dim emp2 As New Employee()
manager.Name = "Zeus"
manager.Level = 2
emp1.Name = "Ares"
emp2.Name = "Artemis"
Dim emps() As Employee = {manager, emp1, emp2}
group.Employees = emps
s.Serialize(writer, group)
writer.Close()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Dim fs As New FileStream(filename, FileMode.Open)
Dim x As New XmlSerializer(GetType(Group))
Dim g As Group = CType(x.Deserialize(fs), Group)
Console.Write("Members:")
Dim e As Employee
For Each e In g.Employees
Console.WriteLine(ControlChars.Tab + e.Name)
Next e
End Sub
End Class
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: