XmlRootAttribute.IsNullable 属性

定义

获取或设置一个值,该值指示 XmlSerializer 是否必须将设置为 null 的成员序列化为设置为 xsi:niltrue 属性。

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

属性值

Boolean

如果 XmlSerializer 生成 true 属性,则为 xsi:nil;否则为 false

示例

The following example serializes a class named Group. 该示例将 XmlRootAttribute 应用于类,并将属性设置为 IsNullable false.

#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 部分:结构第二版

IsNullable如果该属性设置为 true,则会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" />  

适用于