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 --不XmlArrayItemAttributeNestingLevel带值的值应用于第一个数组索引。 只有后续 XmlArrayItemAttribute 对象需要 NestingLevel (指定为 1、2、3 等的值) 。

适用于