XmlAttributes 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表一組屬性物件,用以控制物件序列 XmlSerializer 化與反序列化的方式。
public ref class XmlAttributes
public class XmlAttributes
type XmlAttributes = class
Public Class XmlAttributes
- 繼承
-
XmlAttributes
範例
以下範例序列化了一個名為 Orchestra的類別實例,該類別包含一個名為 Instruments 的欄位,該欄位回傳一個物件陣列 Instrument 。 第二類則 Brass 是繼承自該 Instrument 類。 範例中建立一個 XmlAttributes 物件來覆寫該 Instrument 欄位——允許欄位接受 Brass 物件——並將該 XmlAttributes 物件加入類別的 XmlAttributeOverrides 實例中。
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 element to the collection of elements.
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);
}
}
}
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(ByVal 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 element to the collection of elements.
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.ToString())
Next i
End Sub
End Class
備註
建立 是 XmlAttributes 程序的一部分,會覆寫預設序列化類別實例的方式 XmlSerializer 。 舉例來說,假設你想序列化一個從 DLL 建立且來源無法存取的物件。 透過使用 XmlAttributeOverrides,你可以增強或以其他方式控制物件的序列化方式。
類別 XmlAttributes 的成員直接對應一組控制序列化的屬性類別。 例如, XmlText 屬性必須設為 XmlTextAttribute,這允許你透過指示將 XmlSerializer 屬性值序列化為 XML 文字來覆蓋欄位或屬性的序列化。 關於控制序列化的完整屬性列表,請參見 XmlSerializer。
欲了解更多關於如何與類別一起使用XmlAttributes的XmlAttributeOverrides細節,請參閱「如何:為 XML 串流指定替代元素名稱」。
建構函式
| 名稱 | Description |
|---|---|
| XmlAttributes() |
初始化 XmlAttributes 類別的新執行個體。 |
| XmlAttributes(ICustomAttributeProvider) |
初始化該類別的新實例 XmlAttributes ,並自訂該物件的 XmlSerializer 序列化與反序列化方式。 |
屬性
| 名稱 | Description |
|---|---|
| XmlAnyAttribute |
取得或設定 XmlAnyAttributeAttribute 要覆蓋。 |
| XmlAnyElements |
讓物件集合 XmlAnyElementAttribute 被覆蓋。 |
| XmlArray |
取得或設定一個物件,指定如何 XmlSerializer 序列化公用欄位或回傳陣列的讀寫屬性。 |
| XmlArrayItems |
取得或設定一組物件,指定如何 XmlSerializer 序列化插入於由公開欄位或讀寫屬性回傳的陣列中的項目。 |
| XmlAttribute |
取得或設定一個物件,指定如何 XmlSerializer 序列化公用欄位或公用讀寫屬性,作為 XML 屬性。 |
| XmlChoiceIdentifier |
取得或設定一個物件,讓你能區分一組選擇。 |
| XmlDefaultValue |
取得或設定 XML 元素或屬性的預設值。 |
| XmlElements |
取得一組物件,指定如何 XmlSerializer 序列化公開欄位或以 XML 元素形式讀寫屬性。 |
| XmlEnum |
取得或設定一個物件,指定列舉成員序列化的方式 XmlSerializer 。 |
| XmlIgnore |
取得或設定一個值,指定是否 XmlSerializer 序列化公用欄位或公開讀寫屬性。 |
| Xmlns |
取得或設定一個值,指定當包含回傳 XmlSerializerNamespaces 物件成員的物件被覆寫時,是否保留所有命名空間宣告。 |
| XmlRoot |
取得或設定一個物件,指定如何 XmlSerializer 將類別序列化為 XML 根元素。 |
| XmlText |
取得或設定一個物件,指示將 XmlSerializer 公開欄位或公開讀寫屬性序列化為 XML 文字。 |
| XmlType |
取得或設定一個物件,指定如何 XmlSerializer 序列化已套用的 XmlTypeAttribute 類別。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |