.NET序列化订购 [英] .NET Serialization Ordering

查看:96
本文介绍了.NET序列化订购的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用序列化XmlSerializer的和继承的一些对象,但我有一些问题排序结果。

I am trying to serialize some objects using XmlSerializer and inheritance but I am having some problems with ordering the outcome.

下面是类似于我已经设置一个例子:〜

Below is an example similar to what I have setup: ~

public class SerializableBase
{
    [XmlElement(Order = 1)]
    public bool Property1 { get; set;}

    [XmlElement(Order = 3)]
    public bool Property3 { get; set;}
}

[XmlRoot("Object")]
public class SerializableObject1 : SerializableBase
{
}

[XmlRoot("Object")]
public class SerializableObject2 : SerializableBase
{
    [XmlElement(Order = 2)]
    public bool Property2 { get; set;}
}

我想的结果如下:〜

The outcome I want is as follows: ~

<Object>
    <Property1></Property1>
    <Property2></Property2>
    <Property3></Property3>
</Object>

但是我得到的结果:〜

However I am getting an outcome of: ~

<Object>
    <Property1></Property1>
    <Property3></Property3>
    <Property2></Property2>
</Object>

有谁知道是否有可能或任何替代?

Does anyone know if it is possible or of any alternative?

感谢

推荐答案

从技术上来说,从纯XML的角度来看,我会说,这可能是想要做坏事。

Technically, from a pure xml perspective, I would say that this is probably a bad thing to want to do.

.NET隐藏了之类的东西XmlSerialization的复杂性 - 在这种情况下,它隐藏到您的序列化的XML应符合架构

.NET hides much of the complexity of things like XmlSerialization - in this case, it hides the schema to which your serialized xml should conform.

的推断架构将使用序列元素来描述的基本类型,扩展类型。这需要严格的排序 - 即使是解串器那么严格,并接受订单的元素了。

The inferred schema will use sequence elements to describe the base type, and the extension types. This requires strict ordering -- even if the Deserializer is less strict and accepts out of order elements.

在XML模式定义扩展类型时,从子类中的其他元素一定要来的之后的从基类的元素。

In xml schemas, when defining extension types, the additional elements from the child class must come after the elements from the base class.

你必须要有一个模式,看起来像

you would essentially have a schema that looks something like (xml-y tags removed for clarity)

base
  sequence
    prop1
    prop3

derived1 extends base
  sequence
    <empty>

derived2 extends base
  sequence
    prop2

有没有办法坚持在为prop1和prop3之间的占位符以指示从派生XML属性可以走了。

There's no way to stick a placeholder in between prop1 and prop3 to indicate where the properties from the derived xml can go.

在最后,你有你的数据格式和业务对象之间的不匹配。可能是你最好的选择是定义一个对象来处理你的XML序列化。

In the end, you have a mismatch between your data format and your business object. Probably your best alternative is to define an object to deal with your xml serialization.

例如

[XmlRoot("Object")
public class SerializableObjectForPersistance
{
    [XmlElement(Order = 1)]
    public bool Property1 { get; set; }

    [XmlElement(Order = 2, IsNullable=true)]
    public bool Property2 { get; set; }

    [XmlElement(Order = 3)]
    public bool Property3 { get; set; }
}

该从你的对象模型分开你的XML序列化code。从SerializableObject1或SerializableObject2所有的值复制到SerializableObjectForPersistance,然后序列化。

This separates your xml serialization code from your object model. Copy all the values from SerializableObject1 or SerializableObject2 to SerializableObjectForPersistance, and then serialize it.

从本质上讲,如果你想在你的序列化的XML,这不符合预期的XML序列化框架相当合拍的格式,具体的控制,你需要解耦业务对象设计(继承结构在这种情况下)和责任业务对象的序列化。

Essentially, if you want such specific control over the format of your serialized xml that doesn't quite jive with the expectations xml serialization framework, you need to decouple your business object design (inheritance structure in this case) and the responsibility for serialization of that business object.

这篇关于.NET序列化订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆