Udostępnij za pośrednictwem


XmlArrayItemAttribute.NestingLevel Właściwość

Definicja

Pobiera lub ustawia poziom w hierarchii elementów XML, które XmlArrayItemAttribute mają wpływ.

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

Wartość właściwości

Indeks zerowy zestawu indeksów w tablicy tablic.

Przykłady

Poniższy przykład stosuje trzy XmlArrayItemAttribute atrybuty do tablicy tablic. Aby określić, które tablice mają zastosowanie do każdego atrybutu, NestingLevel właściwość jest ustawiona na indeks tablic.

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);
   }
}

Uwagi

Dokument XML może zawierać hierarchie elementów XML. Aby reprezentować taką hierarchię, jest używana tablica tablic. W takiej tablicy każdy indeks reprezentuje poziom w hierarchii. NestingLevel W związku z tym właściwość jest używana tylko podczas stosowania XmlArrayItemAttribute elementu do pola zwracającego tablicę tablic.

Podczas stosowania atrybutu określ, który poziom hierarchii ma wpływ na atrybut, ustawiając NestingLevelwartość . Pierwszy indeks zawsze ma wartość 0; dlatego opcjonalne jest ustawienie wartości NestingLevel --an XmlArrayItemAttribute bez NestingLevel wartości jest stosowane do pierwszego indeksu tablicy. Tylko kolejne XmlArrayItemAttribute obiekty wymagają NestingLevel określonych wartości (tak jak 1, 2, 3 itd.).

Dotyczy