XmlElementAttribute 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 XmlElementAttribute 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| XmlElementAttribute() |
初始化 XmlElementAttribute 類別的新執行個體。 |
| XmlElementAttribute(String) |
初始化該類別的新實例 XmlElementAttribute ,並指定 XML 元素的名稱。 |
| XmlElementAttribute(Type) |
初始化該類別的新實例 XmlElementAttribute ,並指定套用 的 XmlElementAttribute 成員型別。 此型別用於 XmlSerializer 包含該類型的物件序列化或反序列化時。 |
| XmlElementAttribute(String, Type) |
初始化一個新的實例 XmlElementAttribute ,並指定 XML 元素的名稱及所應用成員 XmlElementAttribute 的派生型別。 此成員類型用於序列 XmlSerializer 化包含它的物件。 |
XmlElementAttribute()
初始化 XmlElementAttribute 類別的新執行個體。
public:
XmlElementAttribute();
public XmlElementAttribute();
Public Sub New ()
範例
以下範例將 應用 XmlElementAttribute 於一個類別。
public class MyClass
{
[XmlElement()]
public string TeacherName;
}
Public Class MyClass1
<XmlElement()> Public TeacherName As String
End Class
適用於
XmlElementAttribute(String)
初始化該類別的新實例 XmlElementAttribute ,並指定 XML 元素的名稱。
public:
XmlElementAttribute(System::String ^ elementName);
public XmlElementAttribute(string elementName);
public XmlElementAttribute(string? elementName);
new System.Xml.Serialization.XmlElementAttribute : string -> System.Xml.Serialization.XmlElementAttribute
Public Sub New (elementName As String)
參數
- elementName
- String
序列化成員的 XML 元素名稱。
範例
以下範例展示了一個包含單一欄位的 Vehicles簡單類別。 範例將 應用 XmlElementAttribute 於欄位,並包含 elementName 參數,指示 產生 XmlSerializer 名為「Cars」而非「Vehicles」的 XML 元素。
public class Transportation
{
[XmlElement("Cars")]
public string Vehicles;
}
Public Class Transportation
<XmlElement("Cars")> Public Vehicles As String
End Class
備註
預設情況下,在序列化類別實例時,使用 XmlSerializer 成員名稱作為 XML 元素名稱。 例如,一個名為 Vehicle 的欄位會產生一個名為 Vehicle的 XML 元素。 但如果你需要其他元素,例如 Cars,請在參數 elementName 中傳遞它。
適用於
XmlElementAttribute(Type)
初始化該類別的新實例 XmlElementAttribute ,並指定套用 的 XmlElementAttribute 成員型別。 此型別用於 XmlSerializer 包含該類型的物件序列化或反序列化時。
public:
XmlElementAttribute(Type ^ type);
public XmlElementAttribute(Type type);
public XmlElementAttribute(Type? type);
new System.Xml.Serialization.XmlElementAttribute : Type -> System.Xml.Serialization.XmlElementAttribute
Public Sub New (type As Type)
參數
範例
以下範例序列化一個名為 Orchestra 的類別,該類別包含一個名為 Instruments的欄位,該欄位回傳一個物件 Instrument 陣列。 第二類則 Brass 是繼承自該 Instrument 類。 範例將 應用 XmlElementAttribute 於 Instruments 欄位,並指定 Brass 類型,允許 Instruments 欄位接受 Brass 物件。 範例中也透過設定 ElementName 屬性指定 XML 元素的名稱。
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)
{
// To write the file, a TextWriter is required.
TextWriter writer = new StreamWriter(filename);
XmlAttributeOverrides attrOverrides =
new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
// Creates an XmlElementAttribute that overrides the Instrument type.
XmlElementAttribute attr = new
XmlElementAttribute(typeof(Brass));
attr.ElementName = "Brass";
// Adds the element to the collection of elements.
attrs.XmlElements.Add(attr);
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Creates the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestra), attrOverrides);
// Creates the object to serialize.
Orchestra band = new Orchestra();
// Creates an object of the derived type.
Brass i = new Brass();
i.Name = "Trumpet";
i.IsValved = true;
Instrument[] myInstruments = {i};
band.Instruments = myInstruments;
s.Serialize(writer,band);
writer.Close();
}
public void DeserializeObject(string filename)
{
XmlAttributeOverrides attrOverrides =
new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
// Creates an XmlElementAttribute that override the Instrument type.
XmlElementAttribute attr = new
XmlElementAttribute(typeof(Brass));
attr.ElementName = "Brass";
// Adds the element to the collection of elements.
attrs.XmlElements.Add(attr);
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Creates 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:");
/* Deserializing differs from serializing. To read the
derived-object values, 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 Strict
Option Explicit
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(filename As String)
' To write the file, a TextWriter is required.
Dim writer As New StreamWriter(filename)
Dim attrOverrides As New XmlAttributeOverrides()
Dim attrs As New XmlAttributes()
' Creates an XmlElementAttribute that overrides the Instrument type.
Dim attr As New XmlElementAttribute(GetType(Brass))
attr.ElementName = "Brass"
' Adds the element to the collection of elements.
attrs.XmlElements.Add(attr)
attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
' Creates the XmlSerializer using the XmlAttributeOverrides.
Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
' Creates the object to serialize.
Dim band As New Orchestra()
' Creates 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
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 that override the Instrument type.
Dim attr As New XmlElementAttribute(GetType(Brass))
attr.ElementName = "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:")
' Deserializing differs from serializing. To read the
' derived-object values, 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
備註
使用參數 type 指定一個由基底類別衍生的型別。 例如,假設一個名為 MyAnimal 的屬性回傳一個 Animal 物件。 你想要強化這個物件,所以你建立一個新類別,這個類別 Mammal 會繼承該 Animal 類別。 要指示 在XmlSerializer序列化MyAnimal該性質時接受該Mammal類別,將類別的 傳遞TypeMammal給建構子。
適用於
XmlElementAttribute(String, Type)
初始化一個新的實例 XmlElementAttribute ,並指定 XML 元素的名稱及所應用成員 XmlElementAttribute 的派生型別。 此成員類型用於序列 XmlSerializer 化包含它的物件。
public:
XmlElementAttribute(System::String ^ elementName, Type ^ type);
public XmlElementAttribute(string elementName, Type type);
public XmlElementAttribute(string? elementName, Type? type);
new System.Xml.Serialization.XmlElementAttribute : string * Type -> System.Xml.Serialization.XmlElementAttribute
Public Sub New (elementName As String, type As Type)
參數
- elementName
- String
序列化成員的 XML 元素名稱。
範例
以下範例序列化一個名為 Orchestra 的類別,該類別包含一個名為 Instruments的欄位,該欄位回傳一個物件 Instrument 陣列。 第二類則 Brass 是繼承自該 Instrument 類。 範例將 應用 XmlElementAttribute 於 Instruments 欄位,並指定 Brass 類型,允許 Instruments 欄位接受 Brass 物件。 範例中也透過設定 ElementName 屬性指定 XML 元素的名稱。
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)
{
// To write the file, a TextWriter is required.
TextWriter writer = new StreamWriter(filename);
XmlAttributeOverrides attrOverrides =
new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
// Creates an XmlElementAttribute that overrides the Instrument type.
XmlElementAttribute attr = new
XmlElementAttribute(typeof(Brass));
attr.ElementName = "Brass";
// Adds the element to the collection of elements.
attrs.XmlElements.Add(attr);
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Creates the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestra), attrOverrides);
// Creates the object to serialize.
Orchestra band = new Orchestra();
// Creates an object of the derived type.
Brass i = new Brass();
i.Name = "Trumpet";
i.IsValved = true;
Instrument[] myInstruments = {i};
band.Instruments = myInstruments;
s.Serialize(writer,band);
writer.Close();
}
public void DeserializeObject(string filename)
{
XmlAttributeOverrides attrOverrides =
new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
// Creates an XmlElementAttribute that override the Instrument type.
XmlElementAttribute attr = new
XmlElementAttribute(typeof(Brass));
attr.ElementName = "Brass";
// Adds the element to the collection of elements.
attrs.XmlElements.Add(attr);
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Creates 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:");
/* Deserializing differs from serializing. To read the
derived-object values, 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 Strict
Option Explicit
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(filename As String)
' To write the file, a TextWriter is required.
Dim writer As New StreamWriter(filename)
Dim attrOverrides As New XmlAttributeOverrides()
Dim attrs As New XmlAttributes()
' Creates an XmlElementAttribute that overrides the Instrument type.
Dim attr As New XmlElementAttribute(GetType(Brass))
attr.ElementName = "Brass"
' Adds the element to the collection of elements.
attrs.XmlElements.Add(attr)
attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
' Creates the XmlSerializer using the XmlAttributeOverrides.
Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
' Creates the object to serialize.
Dim band As New Orchestra()
' Creates 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
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 that override the Instrument type.
Dim attr As New XmlElementAttribute(GetType(Brass))
attr.ElementName = "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:")
' Deserializing differs from serializing. To read the
' derived-object values, 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
備註
預設情況下,在序列化類別實例時,使用 XmlSerializer 成員名稱作為 XML 元素名稱。 例如,一個名為 Vehicle 的欄位會產生一個名為 Vehicle的 XML 元素。 不過,如果你需要其他元素,例如 Cars,請在參數中 elementName 傳遞它。
使用參數 type 指定一個由基底類別衍生的型別。 例如,假設一個名為 MyAnimal 的屬性回傳一個 Animal 物件。 你想要強化這個物件,所以你建立一個新類別,這個類別 Mammal 會繼承該 Animal 類別。 要指示 在XmlSerializer序列化MyAnimal該性質時接受該Mammal類別,將類別的 傳遞TypeMammal給建構子。