简体   繁体   English

C# - 使用XMLSerializer和DataContractSerializer来支持序列化的属性是什么?

[英]C# - what attributes to use to support serializing using both XMLSerializer and DataContractSerializer?

I have some simple POCO object: 我有一些简单的POCO对象:

public class ProductCategoryDTO
{
        public string Name { get; set; }
        public DateTime ModifiedDate { get; set; }
}

As sometimes field order is important (for example, if sending to Infopath forms), I need to keep element order when serializing. 由于有时字段顺序很重要(例如,如果发送到Infopath表单),我需要在序列化时保持元素顺序。

And now I am confused, what attributes I should use for the class and for each field. 现在我很困惑,我应该为班级和每个领域使用什么属性。 I know that: 我知道:

  • DataContractSerializer uses [DataContract] and [DataMember(Order = n)] DataContractSerializer使用[DataContract]和[DataMember(Order = n)]
  • XMLSerializer uses [Serializable] and [XmlElementAttribute(Order = n)]. XMLSerializer使用[Serializable]和[XmlElementAttribute(Order = n)]。

Then what attributes to use if I want to support both XMLSerializer and DataContractSerializer, so it can used in both WCF or ASP. 然后,如果我想支持XMLSerializer和DataContractSerializer,那么要使用哪些属性,因此它可以在WCF或ASP中使用。 web services? 网页服务?

Strictly speaking, you don't need to use any attributes for either ;-p It used to be that DataContractSerializer would demand [DataContract] / [DataMember] (and they absolutely should be used), but you can use it without (but it then acts in a very dubious way similar to BinaryFormatter ). 严格地说 ,你不需要使用任何属性;-p过去, DataContractSerializer需要[DataContract] / [DataMember] (并且绝对应该使用它们),但你可以不使用它(但它然后以类似于BinaryFormatter的非常可疑的方式行事。 Likewise, XmlSerializer doesn't need anything unless you want to control things. 同样,除非你想控制事物,否则XmlSerializer 不需要任何东西。 There are, however, some differences you should note: 但是,您应该注意一些差异:

  • XmlSerializer demands (and uses) a public parameterless constructor; XmlSerializer需要(并使用)公共无参数构造函数; DataContractSerializer doesn't use a constructor (at all). DataContractSerializer 不使用构造函数(根本)。 So watch out for that, and don't (for WCF) rely on code in the ctor - if you have necessary init code, use a serialization callback for WCF. 所以要小心,不要(对于WCF)依赖ctor中的代码 - 如果你有必要的初始化代码,请使用WCF的序列化回调。
  • XmlSerializer demands either public fields (yeuch) or public properties with both get and set (even for lists); XmlSerializer需要公共字段(yeuch)或同时具有getset公共属性(即使对于列表); DataContractSerializer will happily work against private members, properties with (for example) a public get and private set , and collections without a `set (as long as your type initialises it). DataContractSerializer将很乐意使用私有成员,具有(例如)公共get和私有set ,以及没有 set的集合(只要你的类型初始化它)。
  • XmlSerializer demands public types; XmlSerializer需要公共类型; IIRC DataContractSerializer is less fussy IIRC DataContractSerializer不那么繁琐

So yes; 是的 you can support both serializers, and you can add any number of attributes in parallel , but note the above if you want total compatibility. 您可以同时支持两个序列化程序,并且可以并行添加任意数量的属性,但如果您想要完全兼容,请注意上述内容。

Another option is to just use XmlSerializer ; 另一种选择是使用XmlSerializer ; you can configure WCF to use XmlSerializer by using [XmlSerialzerFormat] . 您可以使用[XmlSerialzerFormat]将WCF配置为使用XmlSerializer Both options support inheritance, via [XmlInclude] and [KnownType] . 两个选项都支持继承,通过[XmlInclude][KnownType]

Finally, note that if you implement IXmlSerializable , this takes precedence over either, but it hard to get right. 最后,请注意,如果您实现IXmlSerializable ,这将优先于任何一个,但很难做到正确。 Don't do that unless you have to. 除非必须这样做,否则不要这样做。

I don't see any reason why you couldn't put both attributes on the class and member properties, if you really must. 如果你真的必须,我没有看到为什么你不能将这两个属性放在类和成员属性上的任何原因。 Doesn't look nice, but if it works for you, that's just fine! 看起来不太好看,但如果它对你有用,那就没事了!

[DataContract(Namespace="....")]
[XmlType]
public class ProductCategoryDTO
{
        [DataMember(Order=1)]
        [XmlElementAttribute(Order=1)]
        public string Name { get; set; }

        [DataMember(Order=2)]
        [XmlElementAttribute(Order=2)]
        public DateTime ModifiedDate { get; set; }
}

Order of XML elements should be dictated by the WSDL and you don't need to worry about it. XML元素的顺序应该由WSDL决定,您不必担心它。 Starting from .NET 3.5 SP1 you no longer need to use DataContractAttribute and DataMemberAttribute . 从.NET 3.5 SP1开始,您不再需要使用DataContractAttributeDataMemberAttribute The serializer will automatically include all public properties. 序列化程序将自动包含所有公共属性。 As far as XmlSerializer is concerned, the SerializableAttribute has no effect. XmlSerializer而言, SerializableAttribute没有任何效果。 This attribute is used for binary serialization by the BinaryFormatter . 此属性用于BinaryFormatter的二进制序列化。 So to resume, you could leave the class as a POCO, expose it either in WCF or ASP.NET webservice and leave the clients consume it according to the WSDL. 因此,要恢复,您可以将该类保留为POCO,在WCF或ASP.NET Web服务中公开它,并让客户端根据WSDL使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在单个服务中使用DataContractSerializer和XmlSerializer - Using DataContractSerializer and XmlSerializer both in a single service 如何在同一主机上同时支持DataContractSerializer和XMLSerializer? - How to support both DataContractSerializer and XMLSerializer for the same contract on the same host? 使用XmlSerializer代替DataContractSerializer - Use XmlSerializer Instead of DataContractSerializer 使用XMLSerializer序列化一个类而不显示字段C# - Serializing a class using XMLSerializer not showing fields C# 使用不带Attributes的DataContractSerializer或XmlSerializer删除所有命名空间 - Remove all Namespaces using DataContractSerializer without Attributes or XmlSerializer 实体未使用DataContractSerializer进行序列化 - Entities are not serializing using DataContractSerializer 使用DataContractSerializer序列化大对象图时出现C#StackOverFlowException - C# StackOverFlowException when serializing large object graph with DataContractSerializer XmlSerializer 不序列化两个枚举属性 - XmlSerializer not serializing two enum attributes 使用DataContractSerializer序列化接口列表 - Serializing a List of Interfaces using the DataContractSerializer 在C#中使用XMLSerializer进行序列化时如何包括具有默认值的属性 - How to include properties with default values while serializing with XMLSerializer in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM