共用方式為


XmlArrayItemAttribute.NestingLevel 屬性

定義

取得或設定 XmlArrayItemAttribute 影響的 XML 項目的階層架構中的層級。

public:
 property int NestingLevel { int get(); void set(int value); };
public int NestingLevel { get; set; }
member this.NestingLevel : int with get, set
Public Property NestingLevel As Integer

屬性值

Int32

在陣列組成之陣列的一組索引中,以零起始的索引。

範例

下列範例會將三 XmlArrayItemAttribute 個屬性套用至陣列陣列。 若要指定每個屬性所套用的陣列之哪一個,屬性 NestingLevel 會設定為數組的索引。

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

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

public ref class Forest
{
    // Set the NestingLevel for each array. The first
    // attribute (NestingLevel = 0) is optional.
public:
    [XmlArrayItem(ElementName = "tree", NestingLevel = 0)]
    [XmlArrayItem(ElementName = "branch", NestingLevel = 1)]
    [XmlArrayItem(ElementName = "leaf",NestingLevel = 2)]
    array<array<array<String^>^>^>^ TreeArray;
};

int main()
{
    XmlSerializer^ serializer = gcnew XmlSerializer(Forest::typeid);

    Forest^ constructedForest = gcnew Forest();
    array<array<array<String^>^>^>^ tree = 
        gcnew array<array<array<String^>^>^>(2);

    array<array<String^>^>^ firstBranch = gcnew array<array<String^>^>(1);
    firstBranch[0] = gcnew array<String^>{"One"};
    tree[0] = firstBranch;

    array<array<String^>^>^ secondBranch = gcnew array<array<String^>^>(2);
    secondBranch[0] = gcnew array<String^>{"One","Two"};
    secondBranch[1] = gcnew array<String^>{"One","Two","Three"};
    tree[1] = secondBranch;

    constructedForest->TreeArray = tree;

    serializer->Serialize(Console::Out, constructedForest);
}
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

public class Forest{
   /* Set the NestingLevel for each array. The first
   attribute (NestingLevel = 0) is optional. */
   [XmlArrayItem(ElementName = "tree", NestingLevel = 0)]
   [XmlArrayItem(ElementName = "branch", NestingLevel = 1)]
   [XmlArrayItem(ElementName = "leaf",NestingLevel = 2)]
   public string[][][] TreeArray;
}

public class Test{
   public static void Main(){
      Test t = new Test();
      t.SerializeObject("Tree.xml");
   }
   private void SerializeObject(string filename){
      XmlSerializer serializer =
      new XmlSerializer(typeof(Forest));

      Forest f = new Forest();
      string[][][] myTreeArray = new string[2] [][];

      string[][]myBranchArray1= new string[1][];
      myBranchArray1[0]=new string[1]{"One"};
      myTreeArray[0]=myBranchArray1;

      string[][]myBranchArray2= new string[2][];
      myBranchArray2[0]=new string[2]{"One","Two"};
      myBranchArray2[1]=new string[3]{"One","Two","Three"};
      myTreeArray[1]=myBranchArray2;

      f.TreeArray=myTreeArray;

     serializer.Serialize(Console.Out, f);
   }
}

備註

XML 檔可以包含 XML 元素的階層。 若要表示這類階層,則會使用陣列陣列。 在這類陣列中,每個索引都代表階層中的層級。 因此, NestingLevel 只有在將 套用 XmlArrayItemAttribute 至傳回陣列陣列的欄位時,才會使用 屬性。

套用 屬性時,請藉由設定 NestingLevel 來指定屬性所影響的階層層級。 第一個索引的值一律為 0;因此,選擇性地設定其 NestingLevel --沒有 XmlArrayItemAttribute 值的 會套用至第一個 NestingLevel 陣列索引。 只有後續 XmlArrayItemAttribute 物件需要 NestingLevel (指定為 1、2、3 等的值) 。

適用於