次の方法で共有


XmlRootAttribute.IsNullable プロパティ

XmlSerializer が、 null 参照 (Visual Basic では Nothing) に設定されているメンバを、 true に設定されている xsi:nil 属性にシリアル化するかどうかを示す値を取得または設定します。

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

解説

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

Structures 』を参照してください。

IsNullable プロパティが true に設定されている場合は、次の XML の例で示すように、 xsi:nil 属性が生成されます。

<?xml version="1.0" encoding="utf-8"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:nil="true" />

IsNullable プロパティが false の場合は、次に示すように、空の要素が作成されます。

<?xml version="1.0" encoding="utf-8"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

使用例

[Visual Basic, C#, C++] "Group" という名前のクラスをシリアル化する例を次に示します。この例では、 XmlRootAttribute をクラスに適用し、 IsNullable プロパティを false に設定します。

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


' Apply the XmlRootAttribute and set the IsNullable property to false.
<XmlRoot(IsNullable := False)> _
Public Class Group
    Public Name As String
End Class


Public Class Run
    
    Public Shared Sub Main()
        Console.WriteLine("Running")
        Dim test As New Run()
        test.SerializeObject("NullDoc.xml")
    End Sub     
    
    Public Sub SerializeObject(ByVal filename As String)
        Dim s As New XmlSerializer(GetType(Group))
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create the object to serialize.
        Dim mygroup As Group = Nothing
        
        ' Serialize the object, and close the TextWriter.
        s.Serialize(writer, mygroup)
        writer.Close()
    End Sub
End Class


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

// Apply the XmlRootAttribute and set the IsNullable property to false.
[XmlRoot(IsNullable = false)]
public class Group
{   
   public string Name;
}   


public class Run
{
   public static void Main()
   {
   Console.WriteLine("Running");
      Run test = new Run();
      test.SerializeObject("NullDoc.xml");

   }

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

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

      // Create the object to serialize.
      Group mygroup = null;
      
      // Serialize the object, and close the TextWriter.
      s.Serialize(writer, mygroup);
      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;
using namespace System::Xml;

// Apply the XmlRootAttribute and set the IsNullable property to false.
[XmlRoot(IsNullable = false)]
public __gc class Group
{   
public:
   String* Name;
};   

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

   // Writing the file requires a TextWriter.
   TextWriter* writer = new StreamWriter(filename);

   // Create the object to serialize.
   Group* mygroup = 0;

   // Serialize the object, and close the TextWriter.
   s->Serialize(writer, mygroup);
   writer->Close();
}

int main()
{
   Console::WriteLine(S"Running");
   SerializeObject(S"NullDoc.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

参照

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