Comment décorer/définir les membres de la classe facultative de l'élément XML pour être utilisé avec XmlSerializer?

J'ai le texte suivant de la structure XML. Le theElement élément peut contenir theOptionalList élément, ou pas:

<theElement attrOne="valueOne" attrTwo="valueTwo">
    <theOptionalList>
        <theListItem attrA="valueA" />
        <theListItem attrA="anotherValue" />
        <theListItem attrA="stillAnother" />
    </theOptionalList>
</theElement>
<theElement attrOne="anotherOne" attrTwo="anotherTwo" />

Ce qui est un moyen propre à exprimer le correspondant de la structure de la classe?

Je suis assez sûr de l'suivantes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MyNamespace
{
    public class TheOptionalList
    {
        [XmlAttributeAttribute("attrOne")]
        public string AttrOne { get; set; }

        [XmlAttributeAttribute("attrTwo")]
        public string AttrTwo { get; set; }

        [XmlArrayItem("theListItem", typeof(TheListItem))]
        public TheListItem[] theListItems{ get; set; }

        public override string ToString()
        {
            StringBuilder outText = new StringBuilder();

            outText.Append("attrOne = " + AttrOne + " attrTwo = " + AttrTwo + "\r\n");

            foreach (TheListItem li in theListItems)
            {
                outText.Append(li.ToString());
            }

            return outText.ToString();
        }
    }
}

Ainsi que:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MyNamespace
{
    public class TheListItem
    {
        [XmlAttributeAttribute("attrA")]
        public string AttrA { get; set; }

        public override string ToString()
        {
            StringBuilder outText = new StringBuilder();

            outText.Append("  attrA = " + AttrA + "\r\n");                
            return outText.ToString();
        }
    }
}

Mais ce sujet pour theElement? Dois-je prendre le theOptionalList élément comme un type tableau à lire ce qu'il trouve dans le fichier (soit rien, ou une seule), puis vérifier dans le code si elle est là ou pas? Ou est-il un autre décorateur que je peux fournir? Ou est-il juste de travail?

EDIT: j'ai fini par en utilisant les informations de cette réponse.

OriginalL'auteur John | 2011-10-28