XmlAttributes.XmlDefaultValue 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 XML 元素或屬性的預設值。
public:
property System::Object ^ XmlDefaultValue { System::Object ^ get(); void set(System::Object ^ value); };
public object XmlDefaultValue { get; set; }
public object? XmlDefaultValue { get; set; }
member this.XmlDefaultValue : obj with get, set
Public Property XmlDefaultValue As Object
屬性值
代表 Object XML 元素或屬性的預設值。
範例
以下範例展示了一個名為 Pet class 的類別,其中包含一個預設值為「Dog」的欄位。 然而,範例同時建立一個 XmlAttributes 物件,並將其屬性設 XmlDefaultValue 為新的預設值(「Cat」)。 這會覆蓋原本的預設值。 因此,如果欄位值設為「Cat」,它 XmlSerializer 會將其視為預設值,而不會序列化。 若設定為其他值,則序列 XmlSerializer 化該值。
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;
// This is the class that will be serialized.
public class Pet
{
// The default value for the Animal field is "Dog".
[DefaultValueAttribute("Dog")]
public string Animal ;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("OverrideDefaultValue.xml");
test.DeserializeObject("OverrideDefaultValue.xml");
}
// Return an XmlSerializer used for overriding.
public XmlSerializer CreateOverrider()
{
// Create the XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes xAttrs = new XmlAttributes();
// Add an override for the default value of the GroupName.
Object defaultAnimal= "Cat";
xAttrs.XmlDefaultValue = defaultAnimal;
// Add all the XmlAttributes to the XmlAttributeOverrides object.
xOver.Add(typeof(Pet), "Animal", xAttrs);
// Create the XmlSerializer and return it.
return new XmlSerializer(typeof(Pet), xOver);
}
public void SerializeObject(string filename)
{
// Create an instance of the XmlSerializer class.
XmlSerializer mySerializer = CreateOverrider();
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an instance of the class that will be serialized.
Pet myPet = new Pet();
/* Set the Animal property. If you set it to the default value,
which is "Cat" (the value assigned to the XmlDefaultValue
of the XmlAttributes object), no value will be serialized.
If you change the value to any other value (including "Dog"),
the value will be serialized.
*/
// The default value "Cat" will be assigned (nothing serialized).
myPet.Animal= "";
// Uncommenting the next line also results in the default
// value because Cat is the default value (not serialized).
// myPet.Animal = "Cat";
// Uncomment the next line to see the value serialized:
// myPet.Animal = "fish";
// This will also be serialized because Dog is not the
// default anymore.
// myPet.Animal = "Dog";
// Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myPet);
writer.Close();
}
public void DeserializeObject(string filename)
{
XmlSerializer mySerializer = CreateOverrider();
FileStream fs = new FileStream(filename, FileMode.Open);
Pet myPet= (Pet)mySerializer.Deserialize(fs);
Console.WriteLine(myPet.Animal);
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports System.ComponentModel
' This is the class that will be serialized.
Public Class Pet
' The default value for the Animal field is "Dog".
<DefaultValueAttribute("Dog")> Public Animal As String
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("OverrideDefaultValue.xml")
test.DeserializeObject("OverrideDefaultValue.xml")
End Sub
' Return an XmlSerializer used for overriding.
Public Function CreateOverrider() As XmlSerializer
' Create the XmlAttributeOverrides and XmlAttributes objects.
Dim xOver As New XmlAttributeOverrides()
Dim xAttrs As New XmlAttributes()
' Add an override for the default value of the GroupName.
Dim defaultName As Object = "Cat"
xAttrs.XmlDefaultValue = defaultName
' Add all the XmlAttributes to the XmlAttributeOverrides object.
xOver.Add(GetType(Pet), "Animal", xAttrs)
' Create the XmlSerializer and return it.
Return New XmlSerializer(GetType(Pet), xOver)
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create an instance of the XmlSerializer class.
Dim mySerializer As XmlSerializer = CreateOverrider()
' Writing the file requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Create an instance of the class that will be serialized.
Dim myPet As New Pet()
' Set the Animal property. If you set it to the default value,
' which is "Cat" (the value assigned to the XmlDefaultValue
' of the XmlAttributes object), no value will be serialized.
' If you change the value to any other value (including "Dog"),
' the value will be serialized.
' The default value "Cat" will be assigned (nothing serialized).
myPet.Animal = ""
' Uncommenting the next line also results in the default
' value because Cat is the default value (not serialized).
' myPet.Animal = "Cat";
' Uncomment the next line to see the value serialized:
' myPet.Animal = "fish";
' This will also be serialized because Dog is not the
' default anymore.
' myPet.Animal = "Dog";
' Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myPet)
writer.Close()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Dim mySerializer As XmlSerializer = CreateOverrider()
Dim fs As New FileStream(filename, FileMode.Open)
Dim myPet As Pet = CType(mySerializer.Deserialize(fs), Pet)
Console.WriteLine(myPet.Animal)
End Sub
End Class
備註
你可以透過套用 a DefaultValueAttribute 來指定 XML 元素或 XML 屬性的預設值。 要檢視該值的應用結果,請將應用程式編譯成 DLL 或可執行檔,並將所得檔案作為參數傳遞給 XML 結構定義工具(XSD.exe)。 在 XML 架構文件中,屬性會被賦予 default 預設值。 以下範例中,動物>元素的預設<為「狗」(Dogs)。
<?xml version="1.0"?>
<schema attributeFormDefault="qualified"
elementFormDefault="qualified" targetNamespace=""
xmlns="http://www.w3.org/2000/10/XMLSchema">
<element name="Pets" nullable="true" type="Pets"/>
<complexType name="Pets">
<sequence>
<element default="Dogs" name="Animal" nullable="true"
type="string" minOccurs="0"/>
</sequence>
</complexType>
</schema>
要覆寫預設值,建立 並 Object 指派給 XmlDefaultValue。
如果分配給某欄位或屬性的值等於該欄位或屬性的預設值,則 XmlSerializer 不會將該值序列化到該 XML 實例。 這是因為指派值可以從 XML 架構中恢復。 換句話說,將欄位或屬性設為其預設值,等同於根本不設定。 同樣地,如果欄位或屬性未設定值,則 XmlSerializer 使用結構中的預設值。 在這兩種情況下(將屬性設為預設值,或根本不設定),XML 文件實例都不包含該屬性的任何值。
你可以用類別建構子代替結構來指派預設值。 如果你使用 Xsd.exe 從結構產生類別,你可以註解或移除類別檔案中所有 [System.ComponentModel.DefaultValueAttribute(“myFieldName”]) 屬性。