This documentation is archived and is not being maintained.

XmlArrayItemAttribute.NestingLevel Property

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

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

public int NestingLevel { get; set; }

Property Value

Type: System.Int32
The zero-based index of a set of indexes in an array of arrays.

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).

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.


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



#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::IO;

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

public __gc class Test{
public:
   static void main(){
      Test* t = new Test();
      t->SerializeObject(S"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]{S"One"};
      myTreeArray[0]=myBranchArray1;

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

      f->TreeArray=myTreeArray;

      serializer->Serialize(Console::Out, f);
   }
};

int main(){
   Test::main();
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: