XmlRootAttribute.IsNullable プロパティ

定義

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

public:
 property bool IsNullable { bool get(); void set(bool value); };
public bool IsNullable { get; set; }
member this.IsNullable : bool with get, set
Public Property IsNullable As Boolean

プロパティ値

XmlSerializertrue 属性を生成する場合は xsi:nil。それ以外の場合は false

次の例では、 という名前 Groupのクラスをシリアル化します。 この例では、 XmlRootAttribute を クラスに適用し、 プロパティを IsNullablefalse設定します。

#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 ref class Group
{
public:
   String^ Name;
};

void SerializeObject( String^ filename )
{
   XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );

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

   // Create the object to serialize.
   Group^ mygroup = nullptr;

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

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

注釈

構造体の XML スキーマ仕様を使用すると、XML ドキュメントは要素のコンテンツが見つからないことを明示的に通知できます。 このような要素には、 に設定trueされた 属性xsi:nilが含まれています。 詳細については、「 XML スキーマ パート 1: Structures Second Edition」を参照してください。

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

<?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" />  

適用対象