XmlAttributes.XmlDefaultValue プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlAttributes.XmlDefaultValue プロパティの意味・解説 

XmlAttributes.XmlDefaultValue プロパティ

XML 要素または XML 属性既定値取得または設定します

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Dim instance As XmlAttributes
Dim value As Object

value = instance.XmlDefaultValue

instance.XmlDefaultValue = value
public Object XmlDefaultValue { get; set;
 }
public:
property Object^ XmlDefaultValue {
    Object^ get ();
    void set (Object^ value);
}
/** @property */
public Object get_XmlDefaultValue ()

/** @property */
public void set_XmlDefaultValue (Object value)
public function get XmlDefaultValue
 () : Object

public function set XmlDefaultValue
 (value : Object)

プロパティ
XML 要素または XML 属性既定値を表す Object

解説解説

メンバに 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")] 属性をすべてコメントにするか削除して差し支えありません

使用例使用例

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

Imports System
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 'Main
    
    
    ' 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 XmlAttribueOverrides 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

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 XmlAttribueOverrides 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);
   }
}

#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 XmlAttribueOverrides 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" );
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;
import System.ComponentModel.*;

// This is the class that will be serialized. 
public class Pet
{
    // The default value for the Animal field is "Dog". 
    /** @attribute DefaultValueAttribute("Dog")
     */
    public String animal;
} //Pet

public class Run
{
    public static void main(String[]
 args)
    {
        Run test = new Run();
        test.SerializeObject("OverrideDefaultValue.xml");
        test.DeserializeObject("OverrideDefaultValue.xml");
    } //main

    // 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.set_XmlDefaultValue(defaultAnimal);

        // Add all the XmlAttributes to the XmlAttribueOverrides object.
 
        xOver.Add(Pet.class.ToType(), "Animal", xAttrs);

        // Create the XmlSerializer and return it.
        return new XmlSerializer(Pet.class.ToType(),
 xOver);
    } //CreateOverrider

    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();
    } //SerializeObject

    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);
    } //DeserializeObject
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlAttributes クラス
XmlAttributes メンバ
System.Xml.Serialization 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

XmlAttributes.XmlDefaultValue プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlAttributes.XmlDefaultValue プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS