次の方法で共有


XmlArrayItemAttribute.IsNullable プロパティ

XmlSerializer が、 true に設定されている xsi:nil 属性を持つ空の XML タグとして、メンバをシリアル化するかどうかを示す値を取得または設定します。

Public Property IsNullable As Boolean
[C#]
public bool IsNullable {get; set;}
[C++]
public: __property bool get_IsNullable();public: __property void set_IsNullable(bool);
[JScript]
public function get IsNullable() : Boolean;public function set IsNullable(Boolean);

プロパティ値

XmlSerializerxsi:nil 属性を生成する場合は true 。それ以外の場合は false で、インスタンスは作成されません。既定値は true です。

解説

XML スキーマで構造を指定することにより、XML ドキュメントで、要素の内容が欠落していることを明示的に知らせることができます。このような要素には、 true に設定された属性 xsi:nil が含まれます。詳細については、W3C (World Wide Web Consortium) のサイト (www.w3.org) で『XML Schema Part 1: Structures』という仕様を参照してください。

IsNullable プロパティが true の場合は、 null 参照 (Visual Basic では Nothing) に設定されているクラス メンバのために xsi:nil 属性が生成されます。たとえば、 MyStringArray というフィールドを null 参照 (Nothing) に設定すると、 XmlSerializer は次の XML コードを生成します。

<MyStringArray xsi:nil = "true" />

IsNullable プロパティが false に設定されている場合は、XML 要素が生成されません。

メモ   値型には null 参照 (Nothing) を指定できないため、 IsNullable プロパティは、値型として指定されたメンバには適用できません。

使用例

[Visual Basic, C#, C++] Group というクラスをシリアル化する例を次に示します。このクラスには、 Employee オブジェクトの配列を返す Employees というフィールドが含まれています。 Manager という 2 番目のクラスは、 Employee クラスから派生します。 XmlArrayItemAttribute は、 XmlSerializerEmployee オブジェクトと Manager オブジェクトの両方を配列に挿入できるように指定します。この例では IsNullable プロパティを設定し、その結果、 null 参照 (Visual Basic では Nothing) に設定されている配列では xsi:nil 属性オブジェクトを生成しないように XmlSerializer に指示します。

 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml.Serialization


Public Class Group
    <XmlArray(IsNullable := True), _
     XmlArrayItem(GetType(Manager), IsNullable := False), _
     XmlArrayItem(GetType(Employee), IsNullable := False)> _
    Public Employees() As Employee
End Class

Public Class Employee
    Public Name As String
End Class

Public Class Manager
    Inherits Employee
    Public Level As Integer
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("TypeDoc.xml")
    End Sub    
    
    Public Sub SerializeObject(filename As String)
        Dim s As New XmlSerializer(GetType(Group))
        
        ' To write the file, a TextWriter is required.
        Dim writer As New StreamWriter(filename)
        
        ' Creates the object to serialize.
        Dim group As New Group()
        
        ' Creates a null Manager object.
        Dim mgr As Manager = Nothing
        
        ' Creates a null Employee object.
        Dim y As Employee = Nothing
        
        group.Employees = New Employee() {mgr, y}
        
        ' Serializes the object and closes the TextWriter.
        s.Serialize(writer, group)
        writer.Close()
    End Sub
End Class


[C#] 
using System;
using System.IO;
using System.Xml.Serialization;

public class Group
{   
   [XmlArray(IsNullable = true)]
   [XmlArrayItem(typeof(Manager), IsNullable = false),
   XmlArrayItem(typeof(Employee), IsNullable = false)]
   public Employee[] Employees;
}   

public class Employee
{
   public string Name;
}

public class Manager:Employee
{
   public int Level;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("TypeDoc.xml");
   }

   public void SerializeObject(string filename)
   {
      XmlSerializer s = new XmlSerializer(typeof(Group));

      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);

      // Creates the object to serialize.
      Group group = new Group();

      // Creates a null Manager object.
      Manager mgr = null;
      
      // Creates a null Employee object.
      Employee y = null;
      
      group.Employees = new Employee[2] {mgr, y};

      // Serializes the object and closes the TextWriter.
      s.Serialize(writer, group);
      writer.Close();

   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public __gc class Employee
{
public:
   String* Name;
};

public __gc class Manager:public Employee
{
public:
   int Level;
};

public __gc class Group
{   
public:
   [XmlArray(IsNullable = true)]
   [XmlArrayItem(__typeof(Manager), IsNullable = false),
      XmlArrayItem(__typeof(Employee), IsNullable = false)]
   Employee* Employees[];
};   

void SerializeObject(String* filename)
{
   XmlSerializer* s = new XmlSerializer(__typeof(Group));

   // To write the file, a TextWriter is required.
   TextWriter* writer = new StreamWriter(filename);

   // Creates the object to serialize.
   Group* group = new Group();

   // Creates a null Manager object.
   Manager* mgr = 0;

   // Creates a null Employee object.
   Employee* y = 0;

   Employee* temp[] = {mgr, y};      
   group->Employees = temp;

   // Serializes the object and closes the TextWriter.
   s->Serialize(writer, group);
   writer->Close();

}

int main()
{
   SerializeObject(S"TypeDoc.xml");
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlArrayItemAttribute クラス | XmlArrayItemAttribute メンバ | System.Xml.Serialization 名前空間