XmlArrayItemAttribute.IsNullable 属性

定义

获取或设置一个值,该值指示 XmlSerializer 是否必须将成员序列化为 xsi:nil 属性设置为 true 的 XML 空标记。

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,且不生成实例。 默认值为 true

示例

以下示例序列化名为 Group的类,该类包含一 Employees 个名为返回对象数组的 Employee 字段。 A second class named Manager derives from Employee. 一个 XmlArrayItemAttribute 指定 XmlSerializer 可以将这两个对象 EmployeeManager 对象插入到数组中。 该示例设置该IsNullable属性,从而指示XmlSerializer不生成xsi:nil数组集中的属性对象。null

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public ref class Employee
{
public:
   String^ Name;
};

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

public ref class Group
{
public:

   [XmlArray(IsNullable=true)]
   [XmlArrayItem(Manager::typeid,IsNullable=false),
   XmlArrayItem(Employee::typeid,IsNullable=false)]
   array<Employee^>^Employees;
};

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

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

   // Creates the object to serialize.
   Group^ group = gcnew Group;

   // Creates a null Manager object.
   Manager^ mgr = nullptr;

   // Creates a null Employee object.
   Employee^ y = nullptr;
   array<Employee^>^temp = {mgr,y};
   group->Employees = temp;

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

int main()
{
   SerializeObject( "TypeDoc.xml" );
}
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();
   }
}
Option Explicit
Option Strict

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

注解

结构的 XML 架构规范允许 XML 文档显式指示元素的内容缺失。 此类元素包含设置为true的属性 xsi:nil 。 有关详细信息,请参阅标题为 XML 架构第 1 部分的万维网联盟规范:结构。

IsNullable如果该属性为true,则xsi:nil为已设置为null的类成员生成属性。 例如,如果设置一MyStringArray``null个名为的字段,则XmlSerializer生成以下 XML 代码。

<MyStringArray xsi:nil = "true" />  

IsNullable如果该属性为false,则不会生成 XML 元素。

备注

不能将 IsNullable 属性应用于类型为值类型的成员,因为值类型不能包含 null

适用于