不带 xsi:type 的派生对象的序列化

发布于 2024-11-27 08:35:24 字数 2364 浏览 2 评论 0原文

我在序列化包含派生对象列表的字典时遇到问题。序列化输出包含

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

我希望 BaseAttributes 替换为 Turbine 且 xsi:type 不存在的位置。

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

我的代码总体如下所示。我有一个类 BaseAttributes,从中派生一些类,例如 Turbine 类。这些类存储在带有 BaseAttributes 列表的字典中。该字典是一个已实现的可序列化字典。下面是一般的代码。

[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();
  }
}

我不使用 DataContractor 进行序列化,因为我不会反序列化 XML。我“只是”想创建带有属性的 XML 文件。

我尝试使用 XmlElementOverrides,但在使用过程中可能有些东西我不明白。目前我尝试像这样使用它:

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);

但没有结果。

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:type 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-12-04 08:35:24

今天再次遇到这个问题,很失望,所以没有答案。

如果它是字段或属性中的对象列表,请将其添加到顶部:

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

...

如果它是单个对象,请添加:

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

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))]
小伙你站住 2024-12-04 08:35:24

我已经解决了几乎相同的问题,但与您发布的代码几乎没有区别或没有区别。

您是否尝试将属性作为方面放置在派生元素属性之上?我就是这样做的。此外,我还向我的所有类添加了 [Serializable] 属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文