开发者

Serialization of derived objects without the xsi:type

I'm having a problem when serializing a Dictionary containing a list of derived objects. The serialized output contains

<BaseAttributes xsi:type="Turbine" Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd">

where I would like the BaseAttributes to be substituted with Turbine and the xsi:typ开发者_StackOverflow社区e to be non-existing.

<Turbine Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd">

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.

[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 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.

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:

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 from that.


Ran into it again today and was disapointed SO didn't have the answer.

If it's a list of objects in a field or a property add this on top:

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

...

If it's a single object add:

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


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

Did you tried to place the attributes as aspects, so on top of the derived element property? I do it in that way. Further I also added the [Serializable] attribute to all my classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜