Since you have only a fixed set of possible string values, your schema corresponds to an unbounded sequence of choice elements, where the possible element names correspond to the possible string values.
This can be modeled by replacing the string with some appropriate enum, then making use of the Choice Element Binding Support support built in to XmlSerializer, which requires application of XmlChoiceIdentifierAttribute along with the use of an appropriate serialization surrogate DTO.
First, define an enum that corresponds to the possible string values:
public enum Day
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Next, define the following DTO which will be serialized in place of your List<Day>:
public class DayListDTO
{
[XmlIgnore]
public Day [] Days { get; set; }
[XmlChoiceIdentifier(nameof(DayListDTO.Days))]
[XmlElement(nameof(Day.Monday), typeof(object))]
[XmlElement(nameof(Day.Tuesday), typeof(object))]
[XmlElement(nameof(Day.Wednesday), typeof(object))]
[XmlElement(nameof(Day.Thursday), typeof(object))]
[XmlElement(nameof(Day.Friday), typeof(object))]
[XmlElement(nameof(Day.Saturday), typeof(object))]
[XmlElement(nameof(Day.Sunday), typeof(object))]
public object[] DaysObjects
{
get
{
return Days == null ? null : Enumerable.Repeat(new object(), Days.Length).ToArray();
}
set
{
// Do nothing
}
}
public static implicit operator DayListDTO(List<Day> list)
{
return list == null ? null : new DayListDTO { Days = list.ToArray() };
}
public static implicit operator List<Day>(DayListDTO dayList)
{
return dayList?.Days?.ToList() ?? new List<Day>();
}
}
Finally, make use of the DTO by adding surrogate properties to containing classes as follows:
public class StringArray
{
[XmlIgnore]
public List<Day> Days { get; set; }
[XmlElement("Days")]
public DayListDTO XmlDaysSurrogate { get { return Days; } set { Days = value; } }
}
Which, when serialized, generates the following XML:
<StringArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Days>
<Monday />
<Tuesday />
<Wednesday />
</Days>
</StringArray>
Notes:
To use XmlChoiceIdentifier correctly with an array, your containing DTO type must have two array members:
- One array that records the contents of the choice elements, here
DayListDTO.DaysObjects;
- One array that records the names of the choice elements, here
DayListDTO.Days, in 1-1 correspondence with the first array.
Since the elements corresponding to your list of enums contain no contents at all, a dummy array of object items is returned for the first array.
Using XmlChoiceIdentifier guarantees that the schema which xsd.exe auto-generates for your types will correctly indicate an unbounded sequence of correctly-named choice elements. Other solutions such as implementing custom serialization via IXmlSerializable on some subclass of List<Day> do not have this advantage.
Working sample fiddle here.
[XmlArrayItem("MemberName")].