Sdílet prostřednictvím


XmlAttributeOverrides Třída

Definice

Umožňuje přepsat vlastnosti, pole a třídy atributy při použití XmlSerializer serializace nebo deserializace objektu.

public ref class XmlAttributeOverrides
public class XmlAttributeOverrides
type XmlAttributeOverrides = class
Public Class XmlAttributeOverrides
Dědičnost
XmlAttributeOverrides

Příklady

Následující příklad serializuje třídu pojmenovanou Orchestra, která obsahuje jedno pole s názvem Instruments , které vrací pole Instrument objektů. Druhá třída s názvem Brass dědí z Instrument třídy. Příklad používá instanci XmlAttributeOverrides třídy k přepsání Instrument pole, což pole umožňuje přijímat Brass objekty.

using System;
using System.IO;
using System.Xml.Serialization;

public class Orchestra
{
   public Instrument[] Instruments;
}

public class Instrument
{
   public string Name;
}

public class Brass:Instrument
{
   public bool IsValved;
}

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

    public void SerializeObject(string filename)
    {
      /* Each overridden field, property, or type requires
      an XmlAttributes object. */
      XmlAttributes attrs = new XmlAttributes();

      /* Create an XmlElementAttribute to override the
      field that returns Instrument objects. The overridden field
      returns Brass objects instead. */
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the element to the collection of elements.
      attrs.XmlElements.Add(attr);

      // Create the XmlAttributeOverrides object.
      XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();

      /* Add the type of the class that contains the overridden
      member and the XmlAttributes to override it with to the
      XmlAttributeOverrides object. */
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Create the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create the object that will be serialized.
      Orchestra band = new Orchestra();

      // Create an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;

      // Serialize the object.
      s.Serialize(writer,band);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlAttributeOverrides attrOverrides =
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Create an XmlElementAttribute to override the Instrument.
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the XmlElementAttribute to the collection of objects.
      attrs.XmlElements.Add(attr);

      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Create the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      FileStream fs = new FileStream(filename, FileMode.Open);
      Orchestra band = (Orchestra) s.Deserialize(fs);
      Console.WriteLine("Brass:");

      /* The difference between deserializing the overridden
      XML document and serializing it is this: To read the derived
      object values, you must declare an object of the derived type
      (Brass), and cast the Instrument instance to it. */
      Brass b;
      foreach(Instrument i in band.Instruments)
      {
         b = (Brass)i;
         Console.WriteLine(
         b.Name + "\n" +
         b.IsValved);
      }
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml.Serialization

Public Class Orchestra
    Public Instruments() As Instrument
End Class

Public Class Instrument
    Public Name As String
End Class

Public Class Brass
    Inherits Instrument
    Public IsValved As Boolean
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Override.xml")
        test.DeserializeObject("Override.xml")
    End Sub
        
    Public Sub SerializeObject(ByVal filename As String)
        ' Each overridden field, property, or type requires
        ' an XmlAttributes object. 
        Dim attrs As New XmlAttributes()
        
        ' Create an XmlElementAttribute to override the
        ' field that returns Instrument objects. The overridden field
        ' returns Brass objects instead. 
        Dim attr As New XmlElementAttribute()
        attr.ElementName = "Brass"
        attr.Type = GetType(Brass)
        
        ' Add the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        
        ' Create the XmlAttributeOverrides object.
        Dim attrOverrides As New XmlAttributeOverrides()
        
        ' Add the type of the class that contains the overridden
        ' member and the XmlAttributes to override it with to the
        ' XmlAttributeOverrides object. 
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create the object that will be serialized.
        Dim band As New Orchestra()
        
        ' Create an object of the derived type.
        Dim i As New Brass()
        i.Name = "Trumpet"
        i.IsValved = True
        Dim myInstruments() As Instrument = {i}
        band.Instruments = myInstruments
        
        ' Serialize the object.
        s.Serialize(writer, band)
        writer.Close()
    End Sub    
    
    Public Sub DeserializeObject(filename As String)
        Dim attrOverrides As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()
        
        ' Create an XmlElementAttribute to override the Instrument.
        Dim attr As New XmlElementAttribute()
        attr.ElementName = "Brass"
        attr.Type = GetType(Brass)
        
        ' Add the XmlElementAttribute to the collection of objects.
        attrs.XmlElements.Add(attr)
        
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim band As Orchestra = CType(s.Deserialize(fs), Orchestra)
        Console.WriteLine("Brass:")
        
        ' The difference between deserializing the overridden
        ' XML document and serializing it is this: To read the derived
        ' object values, you must declare an object of the derived type
        ' (Brass), and cast the Instrument instance to it. 
        Dim b As Brass
        Dim i As Instrument
        For Each i In  band.Instruments
            b = CType(i, Brass)
            Console.WriteLine(b.Name & ControlChars.Cr & b.IsValved)
        Next i
    End Sub
End Class

Poznámky

Umožňuje XmlAttributeOverridesXmlSerializer přepsat výchozí způsob serializace sady objektů. Přepsání serializace tímto způsobem má dvě použití: nejprve můžete řídit a rozšířit serializaci objektů nalezených v knihovně DLL - i když nemáte přístup ke zdroji; zadruhé, můžete vytvořit jednu sadu serializovatelných tříd, ale serializovat objekty několika způsoby. Například místo serializace členů instance třídy jako xml elementy, můžete je serializovat jako atributy XML, což vede k efektivnějšímu dokumentu pro přenos.

Po vytvoření objektu XmlAttributeOverrides jej předáte jako argument konstruktoru XmlSerializer . Výsledek XmlSerializer používá data obsažená v XmlAttributeOverrides přepsání atributů, které řídí, jak jsou objekty serializovány. K tomu XmlAttributeOverrides obsahuje kolekci typů objektů, které jsou přepsány, a XmlAttributes také objekt přidružený ke každému přepsání typu objektu. Samotný XmlAttributes objekt obsahuje odpovídající sadu objektů atributů, které řídí, jak jsou jednotlivá pole, vlastnost nebo třída serializována.

Proces vytváření a používání objektu XmlAttributeOverrides je následující:

  1. Vytvořte XmlAttributes objekt.

  2. Vytvořte objekt atributu, který je vhodný pro přepsaný objekt. Chcete-li například přepsat pole nebo vlastnost, vytvořte pomocí XmlElementAttributenového odvozeného typu . Volitelně můžete přiřadit nový ElementNamenebo Namespace přepsání názvu atributu nebo oboru názvů základní třídy.

  3. Přidejte objekt atributu do příslušné XmlAttributes vlastnosti nebo kolekce. Například byste přidali XmlElementAttribute do XmlElements kolekce objektu XmlAttributes a zadali jste název člena, který se přepíše.

  4. Vytvořte XmlAttributeOverrides objekt.

  5. Add Pomocí metody přidejte XmlAttributes objekt do objektuXmlAttributeOverrides. Pokud je přepsaný objekt nebo XmlRootAttributeXmlTypeAttribute, je nutné zadat pouze typ přepsáného objektu. Pokud ale přepisujete pole nebo vlastnost, musíte také zadat název přepsaného člena.

  6. Při vytváření konstruktoru XmlSerializerpředejte XmlAttributeOverrides konstruktoru XmlSerializer .

  7. Použijte výsledek XmlSerializer k serializaci nebo deserializaci odvozené třídy objekty.

Konstruktory

Name Description
XmlAttributeOverrides()

Inicializuje novou instanci XmlAttributeOverrides třídy.

Vlastnosti

Name Description
Item[Type, String]

Získá objekt asociovaný se zadaným typem (základní třída). Parametr člena určuje člen základní třídy, který je přepsán.

Item[Type]

Získá objekt asociovaný se zadaným typem base-class.

Metody

Name Description
Add(Type, String, XmlAttributes)

XmlAttributes Přidá objekt do kolekce XmlAttributes objektů. Parametr type určuje objekt, který se má přepsat. Parametr member určuje název člena, který je přepsán.

Add(Type, XmlAttributes)

XmlAttributes Přidá objekt do kolekce XmlAttributes objektů. Parametr type určuje objekt, který má být přepsán objektem XmlAttributes .

Equals(Object)

Určuje, zda je zadaný objekt roven aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetType()

Získá Type aktuální instance.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Object.

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro

Viz také