XmlAttributes.XmlDefaultValue プロパティ

定義

XML 要素または 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 要素または XML 属性の既定値を表す Object

次の例は、既定値が "Dog" に設定されているフィールドを含む名前 Pet のクラスを示しています。 ただし、この例ではオブジェクトも作成 XmlAttributes し、そのプロパティを XmlDefaultValue 新しい既定値 ("Cat") に設定します。 これにより、元の既定値がオーバーライドされます。 したがって、フィールド値が "Cat" に設定されている場合、 XmlSerializer フィールド値は既定値として扱われ、シリアル化されません。 他の値に設定されている場合は、値を XmlSerializer シリアル化します。

#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::ComponentModel;

// This is the class that will be serialized. 
public ref class Pet
{
public:

   // The default value for the Animal field is "Dog". 

   [DefaultValueAttribute("Dog")]
   String^ Animal;
};

// Return an XmlSerializer used for overriding. 
XmlSerializer^ CreateOverrider()
{
   // Create the XmlAttributeOverrides and XmlAttributes objects. 
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
   XmlAttributes^ xAttrs = gcnew 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( Pet::typeid, "Animal", xAttrs );

   // Create the XmlSerializer and return it.
   return gcnew XmlSerializer( Pet::typeid,xOver );
}

void SerializeObject( String^ filename )
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = CreateOverrider();

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

   // Create an instance of the class that will be serialized. 
   Pet^ myPet = gcnew 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();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Pet^ myPet = dynamic_cast<Pet^>(mySerializer->Deserialize( fs ));
   Console::WriteLine( myPet->Animal );
}

int main()
{
   SerializeObject( "OverrideDefaultValue.xml" );
   DeserializeObject( "OverrideDefaultValue.xml" );
}
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

注釈

メンバーに適用 DefaultValueAttribute することで、XML 要素または XML 属性の既定値を指定できます。 値を適用した結果を調べるには、DLL または実行可能ファイルにアプリケーションをコンパイルし、結果のファイルを引数として XML スキーマ定義ツール (XSD.exe) に渡します。 XML スキーマ ドキュメントでは、属性に既定値が割り当てられます default 。 次の例では、要素の <Animal> 既定値は "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")] 属性をクラス ファイルからコメントアウトまたは削除できます。

適用対象