XmlArrayItemAttribute 构造函数

定义

初始化 XmlArrayItemAttribute 类的新实例。

重载

XmlArrayItemAttribute()

初始化 XmlArrayItemAttribute 类的新实例。

XmlArrayItemAttribute(String)

初始化 XmlArrayItemAttribute 类的新实例,并指定在 XML 文档中生成的 XML 元素的名称。

XmlArrayItemAttribute(Type)

初始化 XmlArrayItemAttribute 类的新实例,并指定可插入到序列化数组中的 Type

XmlArrayItemAttribute(String, Type)

初始化 XmlArrayItemAttribute 类的新实例,并指定在 XML 文档中生成的 XML 元素的名称,以及可插入到所生成的 XML 文档中的 Type

XmlArrayItemAttribute()

初始化 XmlArrayItemAttribute 类的新实例。

public:
 XmlArrayItemAttribute();
public XmlArrayItemAttribute ();
Public Sub New ()

示例

下面的示例序列化一个名为 Transportation 的类,该类包含一 MyVehicles 个名为返回对象数组的 Vehicle 字段。 该示例将应用于XmlArrayItemAttribute字段,允许XmlSerializer将派生自Vehicle该类的Car类实例插入数组中。

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArrayItem,
   XmlArrayItem(Car::typeid,ElementName="Automobile")]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer for the Transportation class. 
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );

   // Creates the object to serialize.
   Transportation^ myTransportation = gcnew Transportation;

   // Creates objects to add to the array.
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^temp = {myVehicle,myCar};
   myTransportation->MyVehicles = temp;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates an XmlSerializer instance.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize( myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length; i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

int main()
{
   SerializeObject( "XmlArrayItem1.xml" );
   DeserializeObject( "XmlArrayItem1.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;
}

public class Transportation
{
   [XmlArrayItem(),
   XmlArrayItem(typeof(Car), ElementName = "Automobile")]
   public Vehicle[] MyVehicles;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("XmlArrayItem1.xml");
      test.DeserializeObject("XmlArrayItem1.xml");
   }

   private void SerializeObject(string filename){
      // Creates an XmlSerializer for the Transportation class.
      XmlSerializer MySerializer = new XmlSerializer(typeof(Transportation));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);

      // Creates the object to serialize.
      Transportation myTransportation = new Transportation();

      // Creates objects to add to the array.
      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";

      myTransportation.MyVehicles =
      new Vehicle[2] {myVehicle, myCar};

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }

   private void DeserializeObject(string filename)
   {
      // Creates an XmlSerializer instance.
      XmlSerializer mySerializer = new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);

      for(int i = 0; i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
 }
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArrayItem(), _
     XmlArrayItem(GetType(Car), ElementName := "Automobile")> _
    Public MyVehicles() As Vehicle
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("XmlArrayItem1.xml")
        test.DeserializeObject("XmlArrayItem1.xml")
    End Sub
    
    
    Private Sub SerializeObject(ByVal filename As String)
        ' Creates an XmlSerializer for the Transportation class. 
        Dim MySerializer As New XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New StreamWriter(filename)
        
        ' Creates the object to serialize.
        Dim myTransportation As New Transportation()
        
        ' Creates objects to add to the array.
        Dim myVehicle As New Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        myTransportation.MyVehicles = New Vehicle() {myVehicle, myCar}
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub
    
    
    Private Sub DeserializeObject(ByVal filename As String)
        ' Create an XmlSerializer instance.
        Dim mySerializer As New XmlSerializer(GetType(Transportation))
        Dim myFileStream As New FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

适用于

XmlArrayItemAttribute(String)

初始化 XmlArrayItemAttribute 类的新实例,并指定在 XML 文档中生成的 XML 元素的名称。

public:
 XmlArrayItemAttribute(System::String ^ elementName);
public XmlArrayItemAttribute (string elementName);
public XmlArrayItemAttribute (string? elementName);
new System.Xml.Serialization.XmlArrayItemAttribute : string -> System.Xml.Serialization.XmlArrayItemAttribute
Public Sub New (elementName As String)

参数

elementName
String

XML 元素的名称。

示例

下面的示例序列化一个名为 Transportation 的类,该类包含一 MyVehicles 个名为返回对象数组的 Vehicle 字段。 该示例将应用于XmlArrayItemAttribute字段,允许XmlSerializer将派生自Vehicle该类的Car类实例插入数组中。 应用特性时,该示例使用elementName参数设置ElementName属性。

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArrayItem(ElementName="Transportation"),
   XmlArrayItem(Car::typeid,ElementName="Automobile")]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer for the Transportation class.
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );
   Transportation^ myTransportation = gcnew Transportation;
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^myVehicles = {myVehicle,myCar};
   myTransportation->MyVehicles = myVehicles;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates the serializer with the type to deserialize.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize( myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length; i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

int main()
{
   SerializeObject( "XmlArrayItem2.xml" );
   DeserializeObject( "XmlArrayItem2.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;
}

public class Transportation
{
   [XmlArrayItem(ElementName = "Transportation"),
   XmlArrayItem(typeof(Car), ElementName = "Automobile")]
   public Vehicle[] MyVehicles;
}

public class Run
{
   public static void Main()
   {
      Run test= new Run();
      test.SerializeObject("XmlArrayItem2.xml");
      test.DeserializeObject("XmlArrayItem2.xml");
   }

   private void SerializeObject(string filename)
   {
      // Creates an XmlSerializer for the Transportation class.
      XmlSerializer MySerializer =
      new XmlSerializer(typeof(Transportation));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);

      Transportation myTransportation = new Transportation();

      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";

      Vehicle [] myVehicles = {myVehicle, myCar};
      myTransportation.MyVehicles = myVehicles;

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }

   private void DeserializeObject(string filename)
   {
      // Creates the serializer with the type to deserialize.
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);

      for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArrayItem(ElementName := "Transportation"), _
     XmlArrayItem(GetType(Car), ElementName := "Automobile")> _
    Public MyVehicles() As Vehicle
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("XmlArrayItem2.xml")
        test.DeserializeObject("XmlArrayItem2.xml")
    End Sub    
    
    Private Sub SerializeObject(ByVal filename As String)
        ' Creates an XmlSerializer for the Transportation class.
        Dim MySerializer As New XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New StreamWriter(filename)
        
        Dim myTransportation As New Transportation()
        
        Dim myVehicle As New Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        Dim myVehicles() As Vehicle = {myVehicle, myCar}
        myTransportation.MyVehicles = myVehicles
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub
        
    Private Sub DeserializeObject(ByVal filename As String)
        ' Create the serializer with the type to deserialize.
        Dim mySerializer As New XmlSerializer(GetType(Transportation))
        Dim myFileStream As New FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

注解

此重载设置 ElementName 属性。

如果希望生成的 XML 元素的名称不同于成员的标识符,请使用此重载。

包含命名空间的 XML 文档可以包含多个版本的元素名称。 有关详细信息,请参见 ElementName 属性。

适用于

XmlArrayItemAttribute(Type)

初始化 XmlArrayItemAttribute 类的新实例,并指定可插入到序列化数组中的 Type

public:
 XmlArrayItemAttribute(Type ^ type);
public XmlArrayItemAttribute (Type type);
public XmlArrayItemAttribute (Type? type);
new System.Xml.Serialization.XmlArrayItemAttribute : Type -> System.Xml.Serialization.XmlArrayItemAttribute
Public Sub New (type As Type)

参数

type
Type

要序列化的对象的 Type

示例

下面的示例序列化一个名为 Transportation 的类,该类包含一 MyVehicles 个名为返回对象数组的 Vehicle 字段。 该示例将应用于XmlArrayItemAttribute字段,允许XmlSerializer将派生自Vehicle该类的Car类实例插入数组中。 应用特性时,该示例使用type参数设置Type属性。

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArrayItem(Vehicle::typeid),
   XmlArrayItem(Car::typeid)]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer. 
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );
   Transportation^ myTransportation = gcnew Transportation;
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^myVehicles = {myVehicle,myCar};
   myTransportation->MyVehicles = myVehicles;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates the serializer with the type to deserialize.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize( myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length; i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

int main()
{
   SerializeObject( "XmlArrayItem3.xml" );
   DeserializeObject( "XmlArrayItem3.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;
}

public class Transportation
{
   [XmlArrayItem(typeof(Vehicle)),
   XmlArrayItem(typeof(Car))]
   public Vehicle[] MyVehicles;
}

public class Run
{
   public static void Main()
   {
      Run test= new Run();
      test.SerializeObject("XmlArrayItem3.xml");
      test.DeserializeObject("XmlArrayItem3.xml");
   }

   private void SerializeObject(string filename)
   {
       // Creates an XmlSerializer.
      XmlSerializer MySerializer =
      new XmlSerializer(typeof(Transportation));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);

      Transportation myTransportation = new Transportation();

      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";

      Vehicle [] myVehicles = {myVehicle, myCar};
      myTransportation.MyVehicles = myVehicles;

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }

   private void DeserializeObject(string filename)
   {
      // Creates the serializer with the type to deserialize.
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);

      for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArrayItem(GetType(Vehicle)), _
     XmlArrayItem(GetType(Car))> _
    Public MyVehicles() As Vehicle
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("XmlArrayItem3.xml")
        test.DeserializeObject("XmlArrayItem3.xml")
    End Sub    
    
    Private Sub SerializeObject(ByVal filename As String)
        ' Creates an XmlSerializer object. 
        Dim MySerializer As New XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New StreamWriter(filename)
        
        Dim myTransportation As New Transportation()
        
        Dim myVehicle As New Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        Dim myVehicles() As Vehicle =  {myVehicle, myCar}
        myTransportation.MyVehicles = myVehicles
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub    
    
    Private Sub DeserializeObject(ByVal filename As String)
        ' Creates the serializer with the type to deserialize.
        Dim mySerializer As New XmlSerializer(GetType(Transportation))
        Dim myFileStream As New FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

适用于

XmlArrayItemAttribute(String, Type)

初始化 XmlArrayItemAttribute 类的新实例,并指定在 XML 文档中生成的 XML 元素的名称,以及可插入到所生成的 XML 文档中的 Type

public:
 XmlArrayItemAttribute(System::String ^ elementName, Type ^ type);
public XmlArrayItemAttribute (string elementName, Type type);
public XmlArrayItemAttribute (string? elementName, Type? type);
new System.Xml.Serialization.XmlArrayItemAttribute : string * Type -> System.Xml.Serialization.XmlArrayItemAttribute
Public Sub New (elementName As String, type As Type)

参数

elementName
String

XML 元素的名称。

type
Type

要序列化的对象的 Type

示例

下面的示例序列化一个名为 Transportation 的类,该类包含一 MyVehicles 个名为返回对象数组的 Vehicle 字段。 该示例将应用于XmlArrayItemAttribute字段,允许XmlSerializer将派生自Vehicle该类的Car类实例插入数组中。 应用特性时,该示例使用elementName参数设置ElementName属性,并使用参数设置Typetype属性。

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public ref class Vehicle
{
public:
   String^ id;
};

public ref class Car: public Vehicle
{
public:
   String^ Maker;
};

public ref class Transportation
{
public:

   [XmlArray]
   [XmlArrayItem("Transport",Vehicle::typeid),
   XmlArrayItem("Automobile",Car::typeid)]
   array<Vehicle^>^MyVehicles;
};

void SerializeObject( String^ filename )
{
   // Creates an XmlSerializer for the Transportation class.
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ myTextWriter = gcnew StreamWriter( filename );
   Transportation^ myTransportation = gcnew Transportation;
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^myVehicles = {myVehicle,myCar};
   myTransportation->MyVehicles = myVehicles;

   // Serializes the object, and closes the StreamWriter.
   MySerializer->Serialize( myTextWriter, myTransportation );
   myTextWriter->Close();
}

void DeserializeObject( String^ filename )
{
   // Creates an XmlSerializer.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Transportation^ myTransportation = dynamic_cast<Transportation^>(mySerializer->Deserialize( myFileStream ));
   for ( int i = 0; i < myTransportation->MyVehicles->Length; i++ )
   {
      Console::WriteLine( myTransportation->MyVehicles[ i ]->id );
   }
}

int main()
{
   SerializeObject( "XmlArrayItem4.xml" );
   DeserializeObject( "XmlArrayItem4.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Vehicle
{
   public string id;
}
public class Car:Vehicle
{
   public string Maker;
}

public class Transportation
{
   [XmlArray]
   [XmlArrayItem("Transport", typeof(Vehicle)),
   XmlArrayItem("Automobile", typeof(Car))]
   public Vehicle[] MyVehicles;
}

public class Run
{
   public static void Main()
   {
      Run test= new Run();
      test.SerializeObject("XmlArrayItem4.xml");
      test.DeserializeObject("XmlArrayItem4.xml");
   }

   private void SerializeObject(string filename)
   {
       // Creates an XmlSerializer for the Transportation class.
      XmlSerializer MySerializer =
      new XmlSerializer(typeof(Transportation));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter myTextWriter = new StreamWriter(filename);

      Transportation myTransportation = new Transportation();

      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";

      Vehicle [] myVehicles = {myVehicle, myCar};
      myTransportation.MyVehicles = myVehicles;

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }

   private void DeserializeObject(string filename)
   {
      // Creates an XmlSerializer.
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);

      for(int i = 0;i < myTransportation.MyVehicles.Length;i++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArray(), _
     XmlArrayItem("Transport", GetType(Vehicle)), _
     XmlArrayItem("Automobile", GetType(Car))> _
    Public MyVehicles() As Vehicle
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("XmlArrayItem4.xml")
        test.DeserializeObject("XmlArrayItem4.xml")
    End Sub    
    
    Private Sub SerializeObject(ByVal filename As String)
        ' Creates an XmlSerializer for the Transportation class.
        Dim MySerializer As New XmlSerializer(GetType(Transportation))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New StreamWriter(filename)
        
        Dim myTransportation As New Transportation()
        
        Dim myVehicle As New Vehicle()
        myVehicle.id = "A12345"
        
        Dim myCar As New Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"
        
        Dim myVehicles() As Vehicle =  {myVehicle, myCar}
        myTransportation.MyVehicles = myVehicles
        
        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
        myTextWriter.Close()
    End Sub    
    
    Private Sub DeserializeObject(ByVal filename As String)
        ' Creates an XmlSerializer.
        Dim mySerializer As New XmlSerializer(GetType(Transportation))
        Dim myFileStream As New FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)
        
        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length - 1
            Console.WriteLine(myTransportation.MyVehicles(i).id)
        Next i
    End Sub
End Class

注解

此重载设置 ElementName 属性和 Type 属性。

如果希望生成的 XML 元素的名称不同于成员的标识符,请使用此重载。

包含命名空间的 XML 文档可以包含多个版本的元素名称。 有关详细信息,请参见 ElementName 属性。

适用于