Changing type of element in XML Serialization

前端 未结 2 944
生来不讨喜
生来不讨喜 2021-01-14 01:15

I am having huge problems with XML serialization. I have two classes, both need to be serializeable. In the inherited class, I would like to change the serialization behavio

相关标签:
2条回答
  • 2021-01-14 01:50

    You're specifying two different XML elements with the same name on the same graph level, which is not allowed.

    I will offer a solution, but it involves concatenating type and name for the Cat class serialization. If it is not imperative to serialize that NameAndType class, then go on: On Animal, set Name to be virtual; On Cat, set XmlIgnore on Name2. Then override Name and return both properties of Name2 the way you fancy.

    If you really need to serialize that class as it is, then I'm afraid you'll have to so it with a different elementName.

    Edit: Sample code:

    public class Animal
    {
        [XmlElement(ElementName = "NAME")]
        public virtual string Name { get; set; }
    
        public virtual bool ShouldSerializeName() { return true; }
    } 
    
    public class Cat : Animal
    {
        public override bool ShouldSerializeName() { return false; }
    
        [XmlIgnore]
        public NameAndType Name2 { get; set; }
    
        [XmlElement(ElementName = "NAME")]
        public override string Name 
        { 
            get
            {
                return String.Format("{0} [Type:{1}]", Name2.Name, Name2.Type); 
            }
            set { } 
        }        
    }
    
    0 讨论(0)
  • 2021-01-14 02:11

    The reason you get the error is that, during XmlSerializer code generation, the code generator doesn't understand that the two potential NAME elements on Cat will never be simultaneously serialized, so throws the exception.

    Instead, you can apply XmlAnyElementAttribute to a virtual property returning an XElement, then manually create and return an appropriate XElement for the name for each class in the hierarchy:

    [XmlInclude(typeof(Cat))]
    public class Animal
    {
        [XmlIgnore]
        public string Name { get; set; }
    
        [XmlAnyElement]
        public virtual XElement XmlName
        {
            get
            {
                return Name == null ? null : new XElement("NAME", Name);
            }
            set
            {
                Name = (value == null ? null : value.Value);
            }
        }
    }
    
    public class Cat : Animal
    {
        // Must be cached as per https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.110%29.aspx
        static XmlSerializer nameSerializer;
    
        static Cat()
        {
            nameSerializer = new XmlSerializer(typeof(NameAndType), new XmlRootAttribute("NAME"));
        }
    
        [XmlIgnore]
        public NameAndType Name2 { get; set; }
    
        [XmlAnyElement]
        public override XElement XmlName
        {
            get
            {
                return (Name2 == null ? null : XObjectExtensions.SerializeToXElement(Name2, nameSerializer, true));
            }
            set
            {
                Name2 = (value == null ? null : XObjectExtensions.Deserialize<NameAndType>(value, nameSerializer));
            }
        }
    }
    

    Using the extension methods:

    public static class XObjectExtensions
    {
        public static T Deserialize<T>(this XContainer element)
        {
            return element.Deserialize<T>(new XmlSerializer(typeof(T)));
        }
    
        public static T Deserialize<T>(this XContainer element, XmlSerializer serializer)
        {
            using (var reader = element.CreateReader())
            {
                object result = serializer.Deserialize(reader);
                if (result is T)
                    return (T)result;
            }
            return default(T);
        }
    
        public static XElement SerializeToXElement<T>(this T obj)
        {
            return obj.SerializeToXElement(new XmlSerializer(obj.GetType()), true);
        }
    
        public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer, bool omitStandardNamespaces)
        {
            var doc = new XDocument();
            using (var writer = doc.CreateWriter())
            {
                XmlSerializerNamespaces ns = null;
                if (omitStandardNamespaces)
                    (ns = new XmlSerializerNamespaces()).Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
                serializer.Serialize(writer, obj, ns);
            }
            var element = doc.Root;
            if (element != null)
                element.Remove();
            return element;
        }
    }
    

    Which, for a List<Animal>, produces XML like this:

    <ArrayOfAnimal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Animal>
            <NAME>duck</NAME>
        </Animal>
        <Animal xsi:type="Cat">
            <NAME>
                <Name>Smokey</Name>
                <Type>Siamese</Type>
            </NAME>
        </Animal>
    </ArrayOfAnimal>
    
    0 讨论(0)
提交回复
热议问题