XmlArrayItemAttribute.NestingLevel Property

Definition

Gets or sets the level in a hierarchy of XML elements that the XmlArrayItemAttribute affects.

C#
public int NestingLevel { get; set; }

Property Value

The zero-based index of a set of indexes in an array of arrays.

Examples

The following example applies three XmlArrayItemAttribute attributes to an array of arrays. To specify which of the arrays each attribute applies to, the NestingLevel property is set to the index of the arrays.

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

Remarks

An XML document can contain hierarchies of XML elements. To represent such a hierarchy, an array of arrays is used. In such an array, each index represents a level in the hierarchy. Therefore, the NestingLevel property is only used when applying an XmlArrayItemAttribute to a field that returns an array of arrays.

When applying the attribute, specify which hierarchy level the attribute affects by setting the NestingLevel. The first index always has the value of 0; therefore it is optional to set its NestingLevel --an XmlArrayItemAttribute without a NestingLevel value is applied to the first array index. Only the subsequent XmlArrayItemAttribute objects require NestingLevel values specified (as 1, 2, 3, and so forth).

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0