XmlElementAttributes Klasa

Definicja

Reprezentuje kolekcję obiektów używanych XmlElementAttribute przez XmlSerializer obiekt , aby zastąpić domyślny sposób serializacji klasy.

public ref class XmlElementAttributes : System::Collections::IList
public ref class XmlElementAttributes : System::Collections::CollectionBase
public class XmlElementAttributes : System.Collections.IList
public class XmlElementAttributes : System.Collections.CollectionBase
type XmlElementAttributes = class
    interface ICollection
    interface IEnumerable
    interface IList
type XmlElementAttributes = class
    inherit CollectionBase
Public Class XmlElementAttributes
Implements IList
Public Class XmlElementAttributes
Inherits CollectionBase
Dziedziczenie
XmlElementAttributes
Dziedziczenie
XmlElementAttributes
Implementuje

Przykłady

Poniższy przykład serializuje klasę Transportation , która zawiera jedno pole o nazwie Vehicles , które zwraca wartość ArrayList. W przykładzie najpierw stosuje się dwa wystąpienia XmlElementAttribute klasy do Vehicles pola, które określa typy obiektów XmlSerializer wstawianych do tablicy. Następnie przykład tworzy dwa XmlElementAttribute obiekty, aby zastąpić zachowanie atrybutów zastosowanych do Vehicles właściwości . Dwa zastępowane obiekty są dodawane do XmlElementAttributes kolekcji obiektu XmlAttributes. Na koniec przykład dodaje element XmlAttributes do XmlAttributeOverridesobiektu , co umożliwia XmlSerializer wstawienie nowych typów obiektów do ArrayList pola zwróconego Vehicles przez to pole.

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

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

public ref class Car
{
public:
   String^ Name;
};

public ref class Plane
{
public:
   String^ Name;
};

public ref class Truck
{
public:
   String^ Name;
};

public ref class Train
{
public:
   String^ Name;
};

public ref class Transportation
{
public:

   // Override these two XmlElementAttributes.

   [XmlElement(Car::typeid),
   XmlElement(Plane::typeid)]
   ArrayList^ Vehicles;
};

XmlSerializer^ CreateOverrider()
{
   // Create XmlAtrributes and XmlAttributeOverrides instances. 
   XmlAttributes^ attrs = gcnew XmlAttributes;
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;

   /* Create an XmlElementAttributes object to override 
      one of the attributes applied to the Vehicles property. */
   XmlElementAttribute^ xElement1 = gcnew XmlElementAttribute( Truck::typeid );

   // Add the XmlElementAttribute to the collection.
   attrs->XmlElements->Add( xElement1 );

   /* Create a second XmlElementAttribute and 
      add it to the collection. */
   XmlElementAttribute^ xElement2 = gcnew XmlElementAttribute( Train::typeid );
   attrs->XmlElements->Add( xElement2 );

   /* Add the XmlAttributes to the XmlAttributeOverrides,
      specifying the member to override. */
   xOver->Add( Transportation::typeid, "Vehicles", attrs );

   // Create the XmlSerializer, and return it.
   XmlSerializer^ xSer = gcnew XmlSerializer( Transportation::typeid,xOver );
   return xSer;
}

void SerializeObject( String^ filename )
{
   // Create an XmlSerializer instance.
   XmlSerializer^ xSer = CreateOverrider();

   // Create the object.
   Transportation^ myTransportation = gcnew Transportation;

   /* Create two new, overriding objects that can be
      inserted into the Vehicles array. */
   myTransportation->Vehicles = gcnew ArrayList;
   Truck^ myTruck = gcnew Truck;
   myTruck->Name = "MyTruck";
   Train^ myTrain = gcnew Train;
   myTrain->Name = "MyTrain";
   myTransportation->Vehicles->Add( myTruck );
   myTransportation->Vehicles->Add( myTrain );
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myTransportation );
}

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

public class Transportation
{
   // Override these two XmlElementAttributes.
   [XmlElement(typeof(Car)),
   XmlElement(typeof(Plane))]
   public ArrayList Vehicles;
}

public class Car
{
   public string Name;
}

public class Plane
{
   public string Name;
}

public class Truck
{
   public string Name;
}

public class Train
{
   public string Name;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("OverrideElement.xml");
   }

   public XmlSerializer CreateOverrider()
   {
      // Create XmlAtrributes and XmlAttributeOverrides instances.

      XmlAttributes attrs = new XmlAttributes();

      XmlAttributeOverrides xOver =
      new XmlAttributeOverrides();

      /* Create an XmlElementAttributes object to override
      one of the attributes applied to the Vehicles property. */
      XmlElementAttribute xElement1 =
      new XmlElementAttribute(typeof(Truck));
      // Add the XmlElementAttribute to the collection.
      attrs.XmlElements.Add(xElement1);

      /* Create a second XmlElementAttribute and
      add it to the collection. */
      XmlElementAttribute xElement2 =
      new XmlElementAttribute(typeof(Train));
      attrs.XmlElements.Add(xElement2);

      /* Add the XmlAttributes to the XmlAttributeOverrides,
      specifying the member to override. */
      xOver.Add(typeof(Transportation), "Vehicles", attrs);

      // Create the XmlSerializer, and return it.
      XmlSerializer xSer = new XmlSerializer
      (typeof(Transportation), xOver);
      return xSer;
   }

   public void SerializeObject(string filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer xSer = CreateOverrider();

      // Create the object.
      Transportation myTransportation =
      new Transportation();

      /* Create two new, overriding objects that can be
      inserted into the Vehicles array. */
      myTransportation.Vehicles = new ArrayList();
      Truck myTruck = new Truck();
      myTruck.Name = "MyTruck";

      Train myTrain = new Train();
      myTrain.Name = "MyTrain";

      myTransportation.Vehicles.Add(myTruck);
      myTransportation.Vehicles.Add(myTrain);

      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myTransportation);
   }
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Xml


Public Class Transportation
    ' Override these two XmlElementAttributes.
    <XmlElement(GetType(Car)), _
     XmlElement(GetType(Plane))> _
    Public Vehicles As ArrayList
End Class

Public Class Car
    Public Name As String
End Class

Public Class Plane
    Public Name As String
End Class

Public Class Truck
    Public Name As String
End Class

Public Class Train
    Public Name As String
End Class

Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("OverrideElement.xml")
    End Sub
    
    
    Public Function CreateOverrider() As XmlSerializer
        ' Create XmlAtrributes and XmlAttributeOverrides instances. 
        Dim attrs As New XmlAttributes()
        
        Dim xOver As New XmlAttributeOverrides()
        
        ' Create an XmlElementAttributes object to override
        ' one of the attributes applied to the Vehicles property. 
        Dim xElement1 As New XmlElementAttribute(GetType(Truck))
        ' Add the XmlElementAttribute to the collection.
        attrs.XmlElements.Add(xElement1)
        
        ' Create a second XmlElementAttribute and
        ' add it to the collection. 
        Dim xElement2 As New XmlElementAttribute(GetType(Train))
        attrs.XmlElements.Add(xElement2)
        
        ' Add the XmlAttributes to the XmlAttributeOverrides,
        ' specifying the member to override. 
        xOver.Add(GetType(Transportation), "Vehicles", attrs)
        
        ' Create the XmlSerializer, and return it.
        Dim xSer As New XmlSerializer(GetType(Transportation), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object.
        Dim myTransportation As New Transportation()
        
        ' Create two new, overriding objects that can be
        ' inserted into the Vehicles array. 
        myTransportation.Vehicles = New ArrayList()
        Dim myTruck As New Truck()
        myTruck.Name = "MyTruck"
        
        Dim myTrain As New Train()
        myTrain.Name = "MyTrain"
        
        myTransportation.Vehicles.Add(myTruck)
        myTransportation.Vehicles.Add(myTrain)
        
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myTransportation)
    End Sub
End Class

Uwagi

Właściwość XmlElementAttributes jest zwracana przez XmlElements właściwość XmlAttributes klasy . Używając XmlAttributeOverrides klasy i XmlAttributes klasy, można zastąpić domyślny sposób XmlSerializer serializacji klasy.

Konstruktory

XmlElementAttributes()

Inicjuje nowe wystąpienie klasy XmlElementAttributes.

Właściwości

Capacity

Pobiera lub ustawia liczbę elementów, które CollectionBase mogą zawierać.

(Odziedziczone po CollectionBase)
Count

Pobiera liczbę elementów zawartych w słowniku ICollection.

Count

Pobiera liczbę elementów zawartych w wystąpieniu CollectionBase . Tej właściwości nie można zastąpić.

(Odziedziczone po CollectionBase)
InnerList

Pobiera obiekt ArrayList zawierający listę elementów w wystąpieniu CollectionBase .

(Odziedziczone po CollectionBase)
Item[Int32]

Pobiera lub ustawia element pod określonym indeksem.

List

Pobiera obiekt IList zawierający listę elementów w wystąpieniu CollectionBase .

(Odziedziczone po CollectionBase)

Metody

Add(XmlElementAttribute)

Dodaje element XmlElementAttribute do kolekcji.

Clear()

Usuwa wszystkie elementy z elementu IList.

Clear()

Usuwa wszystkie obiekty z CollectionBase wystąpienia. Tej metody nie można zastąpić.

(Odziedziczone po CollectionBase)
Contains(XmlElementAttribute)

Określa, czy kolekcja zawiera określony obiekt.

CopyTo(XmlElementAttribute[], Int32)

Kopiuje XmlElementAttributesczęść obiektu lub do tablicy jednowymiarowej.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetEnumerator()

Zwraca moduł wyliczający, który iteruje po kolekcji.

GetEnumerator()

Zwraca moduł wyliczający, który iteruje po wystąpieniu CollectionBase .

(Odziedziczone po CollectionBase)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
IndexOf(XmlElementAttribute)

Pobiera indeks określonego XmlElementAttributeelementu .

Insert(Int32, XmlElementAttribute)

Wstawia element XmlElementAttribute do kolekcji.

MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
OnClear()

Wykonuje dodatkowe procesy niestandardowe podczas czyszczenia zawartości CollectionBase wystąpienia.

(Odziedziczone po CollectionBase)
OnClearComplete()

Wykonuje dodatkowe procesy niestandardowe po wyczyszczeniu zawartości CollectionBase wystąpienia.

(Odziedziczone po CollectionBase)
OnInsert(Int32, Object)

Wykonuje dodatkowe procesy niestandardowe przed wstawieniem nowego elementu do CollectionBase wystąpienia.

(Odziedziczone po CollectionBase)
OnInsertComplete(Int32, Object)

Wykonuje dodatkowe procesy niestandardowe po wstawieniu nowego elementu do CollectionBase wystąpienia.

(Odziedziczone po CollectionBase)
OnRemove(Int32, Object)

Wykonuje dodatkowe procesy niestandardowe podczas usuwania elementu z CollectionBase wystąpienia.

(Odziedziczone po CollectionBase)
OnRemoveComplete(Int32, Object)

Wykonuje dodatkowe procesy niestandardowe po usunięciu CollectionBase elementu z wystąpienia.

(Odziedziczone po CollectionBase)
OnSet(Int32, Object, Object)

Wykonuje dodatkowe procesy niestandardowe przed ustawieniem wartości w wystąpieniu CollectionBase .

(Odziedziczone po CollectionBase)
OnSetComplete(Int32, Object, Object)

Wykonuje dodatkowe procesy niestandardowe po ustawieniu wartości w wystąpieniu CollectionBase .

(Odziedziczone po CollectionBase)
OnValidate(Object)

Wykonuje dodatkowe procesy niestandardowe podczas sprawdzania poprawności wartości.

(Odziedziczone po CollectionBase)
Remove(XmlElementAttribute)

Usuwa określony obiekt z kolekcji.

RemoveAt(Int32)

Usuwa IList element w określonym indeksie.

RemoveAt(Int32)

Usuwa element w określonym indeksie CollectionBase wystąpienia. Ta metoda nie jest zastępowalna.

(Odziedziczone po CollectionBase)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

ICollection.CopyTo(Array, Int32)

Kopiuje elementy ICollection elementu do obiektu Array, zaczynając od określonego Array indeksu.

ICollection.CopyTo(Array, Int32)

Kopiuje całość CollectionBase do zgodnego jednowymiarowego Arrayobiektu , zaczynając od określonego indeksu tablicy docelowej.

(Odziedziczone po CollectionBase)
ICollection.IsSynchronized

Pobiera wartość wskazującą, czy dostęp do elementu ICollection jest synchronizowany (bezpieczny wątk).

ICollection.IsSynchronized

Pobiera wartość wskazującą, czy dostęp do elementu CollectionBase jest synchronizowany (bezpieczny wątk).

(Odziedziczone po CollectionBase)
ICollection.SyncRoot

Pobiera obiekt, który może służyć do synchronizowania dostępu do obiektu ICollection.

ICollection.SyncRoot

Pobiera obiekt, który może służyć do synchronizowania dostępu do obiektu CollectionBase.

(Odziedziczone po CollectionBase)
IList.Add(Object)

Dodaje element do elementu IList.

IList.Add(Object)

Dodaje obiekt na końcu obiektu CollectionBase.

(Odziedziczone po CollectionBase)
IList.Contains(Object)

Określa, czy element IList zawiera określoną wartość.

IList.Contains(Object)

Określa, czy element CollectionBase zawiera określony element.

(Odziedziczone po CollectionBase)
IList.IndexOf(Object)

Określa indeks określonego elementu w elemencie IList.

IList.IndexOf(Object)

Wyszukuje określony Object element i zwraca indeks oparty na zerze pierwszego wystąpienia w całym CollectionBaseobiekcie .

(Odziedziczone po CollectionBase)
IList.Insert(Int32, Object)

Wstawia element do IList obiektu w określonym indeksie.

IList.Insert(Int32, Object)

Wstawia element do określonego indeksu CollectionBase .

(Odziedziczone po CollectionBase)
IList.IsFixedSize

Pobiera wartość wskazującą, czy ma IList stały rozmiar.

IList.IsFixedSize

Pobiera wartość wskazującą, czy ma CollectionBase stały rozmiar.

(Odziedziczone po CollectionBase)
IList.IsReadOnly

Pobiera wartość wskazującą, czy kolekcja IList jest przeznaczona tylko do odczytu.

IList.IsReadOnly

Pobiera wartość wskazującą, czy kolekcja CollectionBase jest przeznaczona tylko do odczytu.

(Odziedziczone po CollectionBase)
IList.Item[Int32]

Pobiera lub ustawia element pod określonym indeksem.

IList.Item[Int32]

Pobiera lub ustawia element pod określonym indeksem.

(Odziedziczone po CollectionBase)
IList.Remove(Object)

Usuwa pierwsze wystąpienie określonego obiektu z obiektu IList.

IList.Remove(Object)

Usuwa pierwsze wystąpienie określonego obiektu z obiektu CollectionBase.

(Odziedziczone po CollectionBase)

Metody rozszerzania

Cast<TResult>(IEnumerable)

Rzutuje elementy elementu IEnumerable na określony typ.

OfType<TResult>(IEnumerable)

Filtruje elementy IEnumerable elementu na podstawie określonego typu.

AsParallel(IEnumerable)

Umożliwia równoległość zapytania.

AsQueryable(IEnumerable)

Konwertuje element IEnumerable na .IQueryable

Dotyczy

Zobacz też