Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialization of derived objects without the xsi:type
    primarykey
    data
    text
    <p>I'm having a problem when serializing a Dictionary containing a list of derived objects. The serialized output contains</p> <pre><code>&lt;BaseAttributes xsi:type="Turbine" Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd"&gt; </code></pre> <p>where I would like the BaseAttributes to be substituted with Turbine and the xsi:type to be non-existing.</p> <pre><code>&lt;Turbine Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd"&gt; </code></pre> <p>My code in overall looks like the following. I have a class BaseAttributes from which I derive some classes for example the Turbine class. These classes are stored in a dictionary with a List of BaseAttributes. The dictionary is an implemented serializable dictionary. Below is the code in general.</p> <pre><code>[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&lt;DateTime, List&lt;BaseAttributes&gt;&gt; units; } [XmlRoot("dictionary")] public class SerilizableUnitsDictionary&lt;TKey, TValue&gt; : Dictionary&lt;TKey, TValue&gt;, 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(); } } </code></pre> <p>I don't use a DataContractor for the serialization, as I will not be deserializing the XML. I "just" want to create the XML file with attributes.</p> <p>I have tried to use the XmlElementOverrides, but there is probably something that I just don' understand in the using. Currently I have tried to use it like this:</p> <pre><code>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); </code></pre> <p>But no result from that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload