I had a problem while serializing a Dictionary containing a list of derived objects. Serialized output contains
<BaseAttributes xsi:type="Turbine" Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd">
where I would like BaseAttributes to be replaced by Turbine, and xsi: the type be non-existent.
<Turbine Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd">
My code as a whole is as follows. I have a BaseAttributes class from which I get some classes, for example, the Turbine class. These classes are stored in the BaseAttributes list dictionary. A dictionary is an implemented serializable dictionary. Below is the code in general.
[XmlInclude(typeof(Turbine)), XmlInclude(typeof(Station)), XmlInclude(typeof(Substation))] public class BaseAttributes { [XmlAttribute("Id")] public Guid Id; } public class Turbine : BaseAttributes { private Element windSpeed; public Element WindSpeed { get { return windSpeed; } set { windSpeed = value; } } public Turbine(float windSpeed){ this.windSpeed= new Element(windSpeed.ToString(),"ms"); }
I do not use DataContractor for serialization, since I will not deserialize XML. I just want to create an XML file with attributes.
I tried using XmlElementOverrides, but there is probably something that I just don't understand when using. Currently, I tried to use it as follows:
XmlAttributes attrs = new XmlAttributes(); XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "Turbine"; attr.Type = typeof(Turbine); attrs.XmlElements.Add(attr); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); attrOverrides.Add(typeof(CollectionOfBaseAttributes ), "BaseAttributes", attrs); XmlSerializer xmlSerializer = new XmlSerializer(typeof(CollectionOfBaseAttributes ),attrOverrides);
But no result.
dennis_ler
source share