XmlAttributeOverrides Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lehetővé teszi a tulajdonság-, mező- és osztályattribútumok felülbírálását egy XmlSerializer objektum szerializálása vagy deszerializálása során.
public ref class XmlAttributeOverrides
public class XmlAttributeOverrides
type XmlAttributeOverrides = class
Public Class XmlAttributeOverrides
- Öröklődés
-
XmlAttributeOverrides
Példák
Az alábbi példa szerializál egy osztálytOrchestra, amely egyetlen, objektumtömböt Instruments visszaadó mezőt Instrument tartalmaz. A második osztály neve Brass öröklődik az Instrument osztálytól. A példa az osztály egy példányát használja a XmlAttributeOverridesInstrument mező felülbírálására, lehetővé téve a mező számára az objektumok elfogadását Brass .
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
Megjegyzések
Ez XmlAttributeOverrides lehetővé teszi, XmlSerializer hogy felülbírálja az objektumok egy készletének szerializálásának alapértelmezett módját. A szerializálás felülírásának két felhasználási módja van: először is szabályozhatja és kiegészítheti a DLL-ben található objektumok szerializálását – még akkor is, ha nincs hozzáférése a forráshoz; másodszor létrehozhat egy sor szerializálható osztályt, de többféleképpen szerializálhatja az objektumokat. Az osztálypéldányok tagjait például XML-elemként szerializálhatja, így azokat XML-attribútumokként szerializálhatja, így hatékonyabb dokumentumot hozhat létre az átvitelhez.
Miután létrehozott egy XmlAttributeOverrides objektumot, argumentumként átadja azt a XmlSerializer konstruktornak. Az eredményül XmlSerializer kapott adatok XmlAttributeOverrides az objektumok szerializálását szabályozó attribútumok felülbírálására használhatók. Ennek elvégzéséhez a XmlAttributeOverrides rendszer a felül bírált objektumtípusok gyűjteményét, valamint az XmlAttributes egyes felülírott objektumtípusokhoz társított objektumokat tartalmazza. XmlAttributes Maga az objektum olyan attribútumobjektumokat tartalmaz, amelyek szabályozzák az egyes mezők, tulajdonságok vagy osztályok szerializálását.
Az objektumok létrehozásának és használatának XmlAttributeOverrides folyamata a következő:
Hozzon létre egy objektumot XmlAttributes .
Hozzon létre egy attribútumobjektumot, amely megfelel a felülírt objektumnak. Ha például felül szeretne bírálni egy mezőt vagy tulajdonságot, hozzon létre egy XmlElementAttribute, az új, származtatott típust. Igény szerint hozzárendelhet egy újat ElementName, vagy Namespace felülírhatja az alaposztály attribútumnevét vagy névterét.
Adja hozzá az attribútumobjektumot a megfelelő XmlAttributes tulajdonsághoz vagy gyűjteményhez. Hozzáadhatja például az XmlElementAttributeXmlElements objektum gyűjteményéhez XmlAttributes , megadva a felülírandó tagnevet.
Hozzon létre egy objektumot XmlAttributeOverrides .
Add A metódus használatával adja hozzá az XmlAttributes objektumot az XmlAttributeOverrides objektumhoz. Ha a felülírt objektum egy XmlRootAttribute vagy XmlTypeAttribute, csak a felülírott objektum típusát kell megadnia. Ha azonban felülír egy mezőt vagy tulajdonságot, meg kell adnia a felülírt tag nevét is.
A konstruktor létrehozásakor XmlSerializeradja át a XmlAttributeOverridesXmlSerializer konstruktornak.
Az eredmény használatával XmlSerializer szerializálhatja vagy deszerializálhatja a származtatott osztályobjektumokat.
Konstruktorok
| Name | Description |
|---|---|
| XmlAttributeOverrides() |
Inicializálja a XmlAttributeOverrides osztály új példányát. |
Tulajdonságok
| Name | Description |
|---|---|
| Item[Type, String] |
Lekéri a megadott (alaposztály) típushoz társított objektumot. A tagparaméter a felülírt alaposztály-tagot adja meg. |
| Item[Type] |
Lekéri a megadott alaposztályhoz tartozó objektumot. |
Metódusok
| Name | Description |
|---|---|
| Add(Type, String, XmlAttributes) |
Objektumot XmlAttributes ad hozzá az objektumgyűjteményhez XmlAttributes . A |
| Add(Type, XmlAttributes) |
Objektumot XmlAttributes ad hozzá az objektumgyűjteményhez XmlAttributes . A |
| Equals(Object) |
Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal. (Öröklődés forrása Object) |
| GetHashCode() |
Ez az alapértelmezett kivonatoló függvény. (Öröklődés forrása Object) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |