I am creating objects dynamically using Activator(C#) and one of these classes looks like:
class Driver
{
Driver() { }
[XmlChoiceIdentifier(\"ItemElementN
ItemElementName
is set to ItemChoiceType.Bit16
because that is the first item in the enumeration. Hence its value is 0
but you can see it as Bit16
. By Activator you creates a new instance. If you don't put arguments in order to set your properties, then their values will be default ones.
I see that you have there XmlChoiceIdentifier and other XmlSerializer's stuff. The purpose of this attribute is to:
ItemElementName
property.ItemElementName
after deserialization based on serialized value of Item
.That's what I can tell you basing on given information...
Here is an example that utilizes XmlSerializer along with XmlChoiceIdentifier:
public class Choices
{
[XmlChoiceIdentifier("ItemType")]
[XmlElement("Text", Type = typeof(string))]
[XmlElement("Integer", Type = typeof(int))]
[XmlElement("LongText", Type = typeof(string))]
public object Choice { get; set; }
[XmlIgnore]
public ItemChoiceType ItemType;
}
[XmlType(IncludeInSchema = false)]
public enum ItemChoiceType
{
Text,
Integer,
LongText
}
class Program
{
static void Main(string[] args)
{
Choices c1 = new Choices();
c1.Choice = "very long text"; // You can put here a value of String or Int32.
c1.ItemType = ItemChoiceType.LongText; // Set the value so that its type match the Choice type (Text or LongText due to type of value is string).
var serializer = new XmlSerializer(typeof(Choices));
using (var stream = new FileStream("Choices.xml", FileMode.Create))
serializer.Serialize(stream, c1);
// Produced xml file.
// Notice:
// 1. LongText as element name
// 2. Choice value inside the element
// 3. ItemType value is not stored
/*
very long text
*/
Choices c2;
using (var stream = new FileStream("Choices.xml", FileMode.Open))
c2 = (Choices)serializer.Deserialize(stream);
// c2.ItemType is restored
}
}