Export (0) Print
Expand All

XmlAttributes.XmlArray Property

Gets or sets an object that specifies how the XmlSerializer serializes a public field or read/write property that returns an array.

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

public XmlArrayAttribute XmlArray { get; set; }
/** @property */
public XmlArrayAttribute get_XmlArray ()

/** @property */
public void set_XmlArray (XmlArrayAttribute value)

public function get XmlArray () : XmlArrayAttribute

public function set XmlArray (value : XmlArrayAttribute)

Property Value

An XmlArrayAttribute that specifies how the XmlSerializer serializes a public field or read/write property that returns an array.

There are two ways in which a public field or public read/write property that returns an array is serialized by the XmlSerializer: the default serialization, and the controlled serialization.

In the default serialization, no attribute is applied to the member. When serialized, the array is serialized as a nested sequence of XML elements with the XML element name of the nested sequence taken from the member name.

To control the serialization more precisely, apply an XmlArrayAttribute to the field or property. For example, you can change the generated XML element name from the default to a different name by setting the ElementName property to a new value.

The XmlArray property allows you to override the default serialization, as well as the serialization controlled by applying an XmlArrayAttribute to the member. For example, you can change the XML element name generated from the default (the member identifier) to a new value. In addition, if you apply an XmlArrayAttribute to a member, it is overridden by any XmlArrayAttribute that is assigned to the XmlArray property.

The following example serializes a class that contains a field named Members that returns an array of objects. The XmlArrayAttribute is used to override the serialization of the field, and rename the element name to Staff.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Group
{
   // This field will be overridden.
   public Member [] Members;
}

public class Member
{
   public string MemberName;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("OverrideArray.xml");
      test.DeserializeObject("OverrideArray.xml");
   }
   // Return an XmlSerializer used for overriding. 
   public XmlSerializer CreateOverrider()
   {
      // Creating XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      XmlAttributes xAttrs = new XmlAttributes();

      // Add an override for the XmlArray.    
      XmlArrayAttribute xArray = new XmlArrayAttribute("Staff");
      xArray.Namespace = "http://www.cpandl.com";
      xAttrs.XmlArray = xArray;
      xOver.Add(typeof(Group), "Members", xAttrs);

      // Create the XmlSerializer and return it.
      return new XmlSerializer(typeof(Group), xOver);
   }
   
 
   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrider();
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      Member m = new Member();
      m.MemberName = "Paul";
      myGroup.Members = new Member[1] {m};
      
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group) 
      mySerializer.Deserialize(fs);
      foreach(Member m in myGroup.Members)
      {
         Console.WriteLine(m.MemberName);
      }
   }
}
   

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// This is the class that will be serialized.
public class Group
{
    // This field will be overridden.
    public Member members[];
} //Group

public class Member
{
    public String memberName;
} //Member

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("OverrideArray.xml");
        test.DeserializeObject("OverrideArray.xml");
    } //main

    // Return an XmlSerializer used for overriding. 
    public XmlSerializer CreateOverrider()
    {
        // Creating XmlAttributeOverrides and XmlAttributes objects.
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
        XmlAttributes xAttrs = new XmlAttributes();

        // Add an override for the XmlArray.    
        XmlArrayAttribute xArray = new XmlArrayAttribute("Staff");
        xArray.set_Namespace("http://www.cpandl.com");
        xAttrs.set_XmlArray(xArray);
        xOver.Add(Group.class.ToType(), "Members", xAttrs);

        // Create the XmlSerializer and return it.
        return new XmlSerializer(Group.class.ToType(), xOver);
    } //CreateOverrider

    public void SerializeObject(String fileName)
    {
        // Create an instance of the XmlSerializer class.
        XmlSerializer mySerializer = CreateOverrider();

        // Writing the file requires a TextWriter.
        TextWriter writer = new StreamWriter(fileName);

        // Create an instance of the class that will be serialized.
        Group myGroup = new Group();

        // Set the object properties.
        Member m = new Member();
        m.memberName = "Paul";
        myGroup.members = new Member[] { m };

        // Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlSerializer mySerializer = CreateOverrider();
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup = (Group)mySerializer.Deserialize(fs);
        for (int iCtr = 0; iCtr < myGroup.members.length; iCtr++) {
            Member m = myGroup.members[iCtr];
            Console.WriteLine(m.memberName);
        }
    } //DeserializeObject
} //Run

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft