Serialization of derived objects without xsi: type

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"); } //used for xmlserilization private Turbine(){} } public class CollectionOfBaseAttributes { public SerilizableUnitsDictionary<DateTime, List<BaseAttributes>> units; } [XmlRoot("dictionary")] public class SerilizableUnitsDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable { public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void WriteXml(System.Xml.XmlWriter writer) { XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue), new XmlRootAttribute("Units")); foreach (TKey key in this.Keys) { writer.WriteStartElement("TimeStamp"); writer.WriteAttributeString("Value", key.ToString()); TValue value = this[key]; foreach (TValue value1 in Values) { valueSerializer.Serialize(writer, value1); } writer.WriteEndElement(); } } 

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.

+7
source share
2 answers

He went into it again today and was disappointed. CO did not have an answer.

If this is a list of objects in a field or property, add this at the top:

 [XmlArrayItem(Type = typeof(Turbine))] [XmlArrayItem(Type = typeof(Station))] 

...

If it is a single object, add:

  [XmlElement(Type = typeof(Turbine))] [XmlElement(Type = typeof(Station))] 
+12
source

I solved almost the same problem, but there is no difference with the code you posted.

You tried to put attributes as aspects, so on top of the derived property? I do it this way. Next, I also added the [Serializable] attribute to all my classes.

-one
source

All Articles