Export (0) Print
Expand All

XmlArrayItemAttribute Constructor

.NET Framework 1.1

Initializes a new instance of the XmlArrayItemAttribute class.

Overload List

Initializes a new instance of the XmlArrayItemAttribute class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public XmlArrayItemAttribute();
[C++] public: XmlArrayItemAttribute();
[JScript] public function XmlArrayItemAttribute();

Initializes a new instance of the XmlArrayItemAttribute class; specifies the name of the XML element generated in the XML document.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public XmlArrayItemAttribute(string);
[C++] public: XmlArrayItemAttribute(String*);
[JScript] public function XmlArrayItemAttribute(String);

Initializes a new instance of the XmlArrayItemAttribute class; specifies the Type that can be inserted into the serialized array.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Type)
[C#] public XmlArrayItemAttribute(Type);
[C++] public: XmlArrayItemAttribute(Type*);
[JScript] public function XmlArrayItemAttribute(Type);

Initializes a new instance of the XmlArrayItemAttribute class; specifies the name of the XML element generated in the XML document, and the Type that can be inserted into the generated XML document.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Type)
[C#] public XmlArrayItemAttribute(string, Type);
[C++] public: XmlArrayItemAttribute(String*, Type*);
[JScript] public function XmlArrayItemAttribute(String, Type);

Example

[Visual Basic, C#, C++] The following example serializes a class named Transportation which contains a field named MyVehicles that returns an array of Vehicle objects. The example applies the XmlArrayItemAttribute to the field, allowing the XmlSerializer to insert instances of the Car class, which is derived from the Vehicle class, into the array. While applying the attribute, the example sets the ElementName property using the elementName parameter, and the Type property using the type parameter.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the XmlArrayItemAttribute constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Option Explicit
Option Strict

Imports System
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


[C#] 
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);
      }
   }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public __gc class Vehicle
{
public:
   String* id;
};
public __gc class Car:public Vehicle
{
public:
   String* Maker;
};

public __gc class Transportation
{  
public:
   [XmlArray]
   [XmlArrayItem(S"Transport", __typeof(Vehicle)), 
      XmlArrayItem(S"Automobile", __typeof(Car))]
   Vehicle* MyVehicles[];
};

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 = S"A12345";

   Car* myCar = new Car();
   myCar->id = S"Car 34";
   myCar->Maker = S"FamousCarMaker";

   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 = 
      new XmlSerializer(__typeof(Transportation));
   FileStream* myFileStream = new 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(S"XmlArrayItem4.xml");
   DeserializeObject(S"XmlArrayItem4.xml");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlArrayItemAttribute Class | XmlArrayItemAttribute Members | System.Xml.Serialization Namespace

Show:
© 2014 Microsoft