This is a note to my future self, and for the benefit of others. The described behavoir is NOT obvious...
I've got this bit of C#:
public enum Choices
{
One,
Two,
Three,
}
public class Element
{
List<Choices> _allowedChoices;
[XmlAttribute]
public List<Choices> AllowedChoices
{
get {return _allowedChoices ?? ( _allowedChoices = new List<Choices>() );}
set { _allowedChoices = value; }
}
}
[Test]
public void testing_empty_enum_list_serialization()
{
var ser = new XmlSerializer(typeof (Element));
using (var sw = new StringWriter())
{
ser.Serialize(sw, new Element
{
AllowedChoices = {},
});
var text = sw.ToString();
Console.WriteLine(text);
using (var sr = new StringReader(text))
{
var deserialized = (Element) ser.Deserialize(sr);
}
}
}
If I use the XmlSerializer to serialize this to xml I get:
( Note the empty AllowedChoices attribute at the end )
<?xml version="1.0" encoding="utf-16"?>
<Element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" AllowedChoices="" />
If I then use the XmlSerializer to deserialize this xml I something like:
System.InvalidOperationException : There is an error in XML document (2, 109).
----> System.InvalidOperationException : Instance validation error: '' is not a valid value for Choices.
This is an empty list of enums that serializes without error, Y U NO deserialize!?