System.InvalidOperationException: Instance validation error: '3' is not a valid value for SomeEnumInMyAppwhich turned out to be quite a google-unfriendly exception. The exception is thrown when trying to serialize an enum with a value that isn't explicitly in the declaration of the enum. A couple of people dealt with this, but I don't really want to manually edit the WSDL, and I don't care right now about breaking a future version of the web service (not likely to happen in my case)
In case nothing of this makes any sense to you, let's see some code:
Given this enum declaration:
public enum Epopo { Uno = 1, Dos = 2 }"Epopo?" Yes, Epopo. Just because.
This test passes:
[Test] public void SerializeEnumToXml1() { Epopo p = Epopo.Uno; using (MemoryStream ms = new MemoryStream()) { XmlSerializer xml = new XmlSerializer(typeof(Epopo)); xml.Serialize(ms, p); } }But this one fails with the mentioned exception:
[Test] public void SerializeEnumToXml2() { Epopo p = Epopo.Uno | Epopo.Dos; using (MemoryStream ms = new MemoryStream()) { XmlSerializer xml = new XmlSerializer(typeof(Epopo)); xml.Serialize(ms, p); } }Hmm. What if we try with a LosFormatter instead of XmlSerializer?
[Test] public void SerializeEnumToLosFormatter() { Epopo p = Epopo.Uno | Epopo.Dos; LosFormatter los = new LosFormatter(); using (MemoryStream ms = new MemoryStream()) { los.Serialize(ms, p); } }Test passes. Damn you, XmlSerializer. What about XmlEnum?
public enum XmlEpopo { [XmlEnum("Uno")] Uno = 1, [XmlEnum("Dos")] Dos = 2 } [Test] public void SerializeEnumWithXmlEnumToXml() { XmlEpopo p = XmlEpopo.Uno | XmlEpopo.Dos; using (MemoryStream ms = new MemoryStream()) { XmlSerializer xml = new XmlSerializer(typeof(XmlEpopo)); xml.Serialize(ms, p); } }Doesn't work either... Well, after that I despaired a bit and started trying everything: XmlElement, PowerCollections.Set, Iesi.Collections.ListSet, Dictionary<Epopo, object>, but nothing worked. Ultimately, I used a List<Epopo>, like this:
[Test] public void SerializeListOfEnum() { List<Epopo> l = new List<Epopo>(); l.Add(Epopo.Uno); l.Add(Epopo.Dos); using (MemoryStream ms = new MemoryStream()) { XmlSerializer xml = new XmlSerializer(typeof(List<Epopo>)); xml.Serialize(ms, l); } }It sucks, I know, but at least it works. If any knows a better solution, feel free to post a comment!
By the way, it seems that Microsoft is having this same problem as well...
Full code for these tests is here.