DataContract与XmlType将

DataContract与XmlType将

本文介绍了DataContract与XmlType将的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于努力学习WCF的一部分,所以我系列化读书了。我努力理解如何,我可以在.NET 3.5控制序列化。举例来说,我有一个简单的类有一些公共属性。通过将DataContract属性的类,我可以如控制命名空间和类的名称,因为它是序列化。

As part of trying to learn WCF, I am reading up on serialization. I am struggling to understand how I can control serialization in .NET 3.5. For instance, I have a simple class with a few public properties. By adding the DataContract attribute to that class, I can for instance control the namespace and the name of the class as it is serialized.

在另一方面,我可以添加Serializable属性(可能甚至没有必要)和一个XMLType属性,这也使我能够控制的命名空间和一个用于序列化类的名称。

On the other hand, I could add the Serializable attribute (probably not even necessary) and an XmlType attribute, which also allows me to control the namespace and the name that is used for serializing the class.

我实现了这两种方法,并使用类中的ServiceContract作为接口调用的一部分。然后我用一个Http分析,看看不同的对象serizalized,我注意到XmlType将不影响XML在HTTP的。

I implemented both approaches and use the class in a ServiceContract as part of an interface call. I then use a Http analyzer to see how the various objects are serizalized and I noticed that the XmlType did not influence the xml in the http at all.

我一直在试图理解这一切的一天。我在想什么?

I have been trying to understand this all day. What am I missing?

更新:我明白两个,为什么他们在那里的区别。我只是不明白,为什么我不能影响生成的XML与XmlType将或(只是试过XmlRoot)。

Update:I do understand the difference between the two and why they are there. I just don't understand why I cannot influence the generated xml with XmlType or (just tried it XmlRoot).

基本上,你可以通过实现IXmlSerializable的,除了名称空间和顶级元素的名称控制序列的所有细节。对于这一点,我是假设你需要进XML或XmlRoot属性。难道我错了吗?

Basically, you can control all details of the serialization by implementing IXmlSerializable, except the namespaces and the name of the top level element. For that, I was assuming that you would need the XmlType or XmlRoot attribute. Was I wrong?

推荐答案

DataContractSerializer的主要的一点是要的没有的控制序列的详细信息。相反,这个想法是将数据序列化到可以由数量最多的客户消费形式。

The main point of the DataContractSerializer is to not control the details of serialization. Instead, the idea is to serialize your data into a form that can be consumed by the largest number of clients.

而不是被关注的模式的细节,一个限定在所述数据成员的术语数据契约被发送和接收。它是数据的一个非常抽象描述。它被序列化到一个反映的抽象描述非常简单的格式。

Instead of being concerned with the details of the schema, one defines a data contract in terms of the data members to be sent and received. It is a very abstract description of the data. It is serialized into a very simple format that reflects the abstract description.

XML序列化应只在这里你绝对需要在XML的细节控制,序列化和反序列化。当你不需要那么多的控制,坚持与数据协定序列化。

The XML Serializer should be used only where you absolutely require control over the details of the XML to be serialized or deserialized. When you don't need that much control, stick with the Data Contract Serializer.

这篇关于DataContract与XmlType将的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:58