Create object based on XmlChoiceIdentifier

前端 未结 1 523
春和景丽
春和景丽 2021-01-25 05:16

I am creating objects dynamically using Activator(C#) and one of these classes looks like:

class Driver
{
   Driver() { }

   [XmlChoiceIdentifier(\"ItemElementN         


        
相关标签:
1条回答
  • 2021-01-25 05:49

    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:

    1. Do not serialize ItemElementName property.
    2. To restore 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
            /*
            <?xml version="1.0"?>
            <Choices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <LongText>very long text</LongText>
            </Choices>
            */
    
            Choices c2;
            using (var stream = new FileStream("Choices.xml", FileMode.Open))
                c2 = (Choices)serializer.Deserialize(stream);
    
            // c2.ItemType is restored
        }
    }
    
    0 讨论(0)
提交回复
热议问题